Spring整合Mybatis实例

Spring整合Mybatis实例,第1张

Spring整合Mybatis实例

//本文章参看BliBli学习总结,感谢提供视频的北京动力节点,本文章仅分享供大家参考学习,没有商业用途,谢谢!

Spring整合mybatis 1、创建数据库springdb,新建表Student

2、新建maven项目,选maven-archetype-quickstart

Groupid:com.bjpowernode

3、删除默认创建的App类文件以及AppTest测试文件<不删也可以> 4、pom.xml文件中加入依赖



  4.0.0

  com.bjpowernode
  spring_mybatis
  1.0-SNAPSHOT

  spring_mybatis
  
  http://www.example.com

  
    UTF-8
    1.8
    1.8
  

  
    
    
      junit
      junit
      4.11
      test
    
    
    
      org.springframework
      spring-context
      5.2.5.RELEASE
    
    
    
      org.springframework
      spring-tx
      5.2.5.RELEASE
    
    
      org.springframework
      spring-jdbc
      5.2.5.RELEASE
    
    
    
      org.mybatis
      mybatis
      3.5.1
    
    
    
      org.mybatis
      mybatis-spring
      1.3.1
    
    
    
      mysql
      mysql-connector-java
      5.1.9
    
    
    
      com.alibaba
      druid
      1.1.12
    
  

  
    
    
      
        src/main/java
        
          ***.xml
        
        false
      
    
    
    
      
        maven-compiler-plugin
        3.1
        
          1.8
          1.8
        
      
    
  


5、新建domain包并定义实体类Student.java
package com.bjpowernode.domain;

public class Student {
    //属性名和列名一样。
    private Integer id;
    private String name;
    private String email;
    private Integer age;

    public Student() {
    }

    public Student(Integer id, String name, String email, Integer age) {
        this.id = id;
        this.name = name;
        this.email = email;
        this.age = age;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + ''' +
                ", email='" + email + ''' +
                ", age=" + age +
                '}';
    }
}

6、新建dao包并定义StudentDao.java接口
package com.bjpowernode.dao;

import com.bjpowernode.domain.Student;

import java.util.List;

public interface StudentDao {

    int insertStudent(Student student);
    List selectStudents();
}

7、在dao包中定义映射文件StudentDao.xml




    
        insert into student values(#{id},#{name},#{email},#{age})
    

    




8、新建service包并定义Service.java接口和ServiceImpl.java实现类
package com.bjpowernode.service;

import com.bjpowernode.domain.Student;

import java.util.List;

public interface StudentService {

    int addStudent(Student student);
    List queryStudents();
}

package com.bjpowernode.service.impl;

import com.bjpowernode.dao.StudentDao;
import com.bjpowernode.domain.Student;
import com.bjpowernode.service.StudentService;

import java.util.List;

public class StudentServiceImpl implements StudentService {

    //引用类型
    private StudentDao studentDao;

    //使用set注入,赋值
    public void setStudentDao(StudentDao studentDao) {
        this.studentDao = studentDao;
    }

    @Override
    public int addStudent(Student student) {
        int nums = studentDao.insertStudent(student);
        return nums;
    }

    @Override
    public List queryStudents() {
        List students = studentDao.selectStudents();
        return students;
    }
}

9、main文件夹下新建resources包,并标记为Resources根并定义Mybatis.xml主配置文件



    
    
        
        
    

    
    
        
        
    


    
    
        
        
    



10、resources包下新建applicationContext.xml Spring配置文件



    
    

    
    
        
        
        
        
        
        
    

    
    
        
        
        
        
    

    
    
        
        
        
        
    

    
    
        
    


10、新建jdbc.properties文件
jdbc.url=jdbc:mysql://localhost:3306/springdb
jdbc.username=root
jdbc.passwd=root
jdbc.max=30
11、test/ java/com/bjpowernode下新建测试类MyTest.java
package com.bjpowernode;

import com.bjpowernode.dao.StudentDao;
import com.bjpowernode.domain.Student;
import com.bjpowernode.service.StudentService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class MyTest {
    @Test
    public void test01(){

        String config="applicationContext.xml";
        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
        String names[] = ctx.getBeanDefinitionNames();
        for(String na:names){
            System.out.println("容器中对象名称:"+na+"|"+ctx.getBean(na));
        }
    }

    @Test
    public void testDaoInsert(){

        String config="applicationContext.xml";
        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
        //获取spring容器中的dao对象
        StudentDao dao  = (StudentDao) ctx.getBean("studentDao");
        Student student  = new Student();
        student.setId(1013);
        student.setName("周锋");
        student.setEmail("zhoufeng@qq.com");
        student.setAge(20);
        int nums = dao.insertStudent(student);
        System.out.println("nums="+nums);
    }

    @Test
    public void testServiceInsert(){

        String config="applicationContext.xml";
        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
        //获取spring容器中的dao对象
        StudentService service = (StudentService) ctx.getBean("studentService");
        Student student  = new Student();
        student.setId(1015);
        student.setName("李胜利");
        student.setEmail("zhoufeng@qq.com");
        student.setAge(26);
        int nums = service.addStudent(student);
        //spring和mybatis整合在一起使用,事务是自动提交的。 无需执行SqlSession.commit();
        System.out.println("nums="+nums);
    }

    @Test
    public void testServiceSelect(){

        String config="applicationContext.xml";
        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
        //获取spring容器中的dao对象
        StudentService service = (StudentService) ctx.getBean("studentService");

        List students = service.queryStudents();
        for (Student stu:students){
            System.out.println(stu);
        }
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4658418.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-06
下一篇 2022-11-06

发表评论

登录后才能评论

评论列表(0条)

保存