Java——Spring整合Mybatis(IDEA版)

本文适用于初学者:

该文主要教大家如何整合spring和mybatis,整合完成效果,可以从数据库中查询出学生信息:

 

完整的工程目录如下:

 

整合思路:

  • 需要spring来管理数据源信息。
  • 需要spring通过单例方式管理SqlSessionFactory。
  • 使用SqlSessionFactory创建SqlSession。(spring和mybatis整合自动完成)
  • 持久层的mapper都需要由spring进行管理,spring和mybatis整合生成mapper代理对象。

 

下面开始工程搭建:

 

第一步:创建工程 File—New—Project

 

 

 

 

 

点击Finish完成

 

注意:项目创建完成会有如下图提示:

 

 这里是问你是否要自动导入,选择Enable即可,maven便会自动帮你导入jar包

 

第二步:项目准备

本项目需要用到mysql数据库,首先先在mysql中创建一个数据库,然后创建一张表,sql语句如下:

 1 CREATE DATABASE /*!32312 IF NOT EXISTS*/`test` /*!40100 DEFAULT CHARACTER SET utf8 */;
 2 
 3 USE `test`;
 4 
 5 /*Table structure for table `student` */
 6 
 7 DROP TABLE IF EXISTS `student`;
 8 
 9 CREATE TABLE `student` (
10   `id` int(11) DEFAULT NULL,
11   `username` varchar(20) DEFAULT NULL,
12   `password` varchar(20) DEFAULT NULL,
13   `email` varchar(20) DEFAULT NULL
14 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
15 
16 /*Data for the table `student` */
17 
18 insert  into `student`(`id`,`username`,`password`,`email`) values 
19 (1,'jack','123','123@jack'),
20 (2,'rose','520','rose@123');
sql语句

 

第三步:正式开发

(1)、修改pom文件

  1 xml version="1.0" encoding="UTF-8"?>
  2 <project xmlns="http://maven.apache.org/POM/4.0.0"
  3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5     <modelVersion>4.0.0modelVersion>
  6 
  7     <groupId>cn.grandagegroupId>
  8     <artifactId>SpringMybatisTestartifactId>
  9     <version>1.0-SNAPSHOTversion>
 10 
 11     <properties>
 12         
 13         <spring.version>4.1.6.RELEASEspring.version>
 14         
 15         <mybatis.version>3.2.6mybatis.version>
 16         
 17         <slf4j.version>1.7.7slf4j.version>
 18         <log4j.version>1.2.17log4j.version>
 19     properties>
 20     <dependencies>
 21         <dependency>
 22             <groupId>junitgroupId>
 23             <artifactId>junitartifactId>
 24             <version>4.11version>
 25             
 26             <scope>testscope>
 27         dependency>
 28         
 29         <dependency>
 30             <groupId>org.springframeworkgroupId>
 31             <artifactId>spring-coreartifactId>
 32             <version>${spring.version}version>
 33         dependency>
 34         <dependency>
 35             <groupId>org.springframeworkgroupId>
 36             <artifactId>spring-webartifactId>
 37             <version>${spring.version}version>
 38         dependency>
 39         <dependency>
 40             <groupId>org.springframeworkgroupId>
 41             <artifactId>spring-oxmartifactId>
 42             <version>${spring.version}version>
 43         dependency>
 44         <dependency>
 45             <groupId>org.springframeworkgroupId>
 46             <artifactId>spring-txartifactId>
 47             <version>${spring.version}version>
 48         dependency>
 49         <dependency>
 50             <groupId>org.springframeworkgroupId>
 51             <artifactId>spring-jdbcartifactId>
 52             <version>${spring.version}version>
 53         dependency>
 54         <dependency>
 55             <groupId>org.springframeworkgroupId>
 56             <artifactId>spring-webmvcartifactId>
 57             <version>${spring.version}version>
 58         dependency>
 59         <dependency>
 60             <groupId>org.springframeworkgroupId>
 61             <artifactId>spring-aopartifactId>
 62             <version>${spring.version}version>
 63         dependency>
 64         <dependency>
 65             <groupId>org.springframeworkgroupId>
 66             <artifactId>spring-context-supportartifactId>
 67             <version>${spring.version}version>
 68         dependency>
 69         <dependency>
 70             <groupId>org.springframeworkgroupId>
 71             <artifactId>spring-testartifactId>
 72             <version>${spring.version}version>
 73         dependency>
 74         
 75         <dependency>
 76             <groupId>org.mybatisgroupId>
 77             <artifactId>mybatisartifactId>
 78             <version>${mybatis.version}version>
 79         dependency>
 80         
 81         <dependency>
 82             <groupId>org.mybatisgroupId>
 83             <artifactId>mybatis-springartifactId>
 84             <version>1.2.2version>
 85         dependency>
 86         
 87         <dependency>
 88             <groupId>javaxgroupId>
 89             <artifactId>javaee-apiartifactId>
 90             <version>7.0version>
 91         dependency>
 92         
 93         <dependency>
 94             <groupId>mysqlgroupId>
 95             <artifactId>mysql-connector-javaartifactId>
 96             <version>5.1.39version>
 97         dependency>
 98         
 99         <dependency>
100             <groupId>c3p0groupId>
101             <artifactId>c3p0artifactId>
102             <version>0.9.1.2version>
103         dependency>
104         
105         <dependency>
106             <groupId>commons-dbcpgroupId>
107             <artifactId>commons-dbcpartifactId>
108             <version>1.2.2version>
109         dependency>
110         
111         <dependency>
112             <groupId>jstlgroupId>
113             <artifactId>jstlartifactId>
114             <version>1.2version>
115         dependency>
116         
117         
118         <dependency>
119             <groupId>log4jgroupId>
120             <artifactId>log4jartifactId>
121             <version>${log4j.version}version>
122         dependency>
123         
124         <dependency>
125             <groupId>com.alibabagroupId>
126             <artifactId>fastjsonartifactId>
127             <version>1.1.41version>
128         dependency>
129         <dependency>
130             <groupId>org.slf4jgroupId>
131             <artifactId>slf4j-apiartifactId>
132             <version>${slf4j.version}version>
133         dependency>
134         <dependency>
135             <groupId>org.slf4jgroupId>
136             <artifactId>slf4j-log4j12artifactId>
137             <version>${slf4j.version}version>
138         dependency>
139         
140         
141         <dependency>
142             <groupId>org.codehaus.jacksongroupId>
143             <artifactId>jackson-mapper-aslartifactId>
144             <version>1.9.13version>
145         dependency>
146         
147         <dependency>
148             <groupId>commons-fileuploadgroupId>
149             <artifactId>commons-fileuploadartifactId>
150             <version>1.3.1version>
151         dependency>
152         <dependency>
153             <groupId>commons-iogroupId>
154             <artifactId>commons-ioartifactId>
155             <version>2.4version>
156         dependency>
157         <dependency>
158             <groupId>commons-codecgroupId>
159             <artifactId>commons-codecartifactId>
160             <version>1.9version>
161         dependency>
162     dependencies>
163 
164 
165 project>
POM文件

(2)、新建实体类Student:

 1 package cn.grandage.pojo;
 2 
 3 public class Student {
 4 
 5     private Integer id;
 6     private String username;
 7     private String password;
 8     private String email;
 9 
10     public Integer getId() {
11         return id;
12     }
13 
14     public void setId(Integer id) {
15         this.id = id;
16     }
17 
18     public String getUsername() {
19         return username;
20     }
21 
22     public void setUsername(String username) {
23         this.username = username;
24     }
25 
26     public String getPassword() {
27         return password;
28     }
29 
30     public void setPassword(String password) {
31         this.password = password;
32     }
33 
34     public String getEmail() {
35         return email;
36     }
37 
38     public void setEmail(String email) {
39         this.email = email;
40     }
41 
42     @Override
43     public String toString() {
44         return "StudentMapper{" +
45                 "id=" + id +
46                 ", username='" + username + '\'' +
47                 ", password='" + password + '\'' +
48                 ", email='" + email + '\'' +
49                 '}';
50     }
51 }
Student

(3)、新建db.properties文件

1 db.driver=com.mysql.jdbc.Driver
2 #数据库连接字符串(改成自己的连接)
3 db.url=jdbc:mysql://localhost:3306/test
4 #数据库用户名(这里改成自己的用户名)
5 db.username=root
6 #数据库密码(这里改成自己的密码)
7 db.password=123456
db.properties

(4)、新建StudentMapper接口

1 package cn.grandage.mapper;
2 
3 
4 import cn.grandage.pojo.Student;
5 
6 public interface StudentMapper {
7 
8     public Student findStudentById(int id);
9 }
StudentMapper

(5)、新建mybatis映射配置文件:

 1 xml version="1.0" encoding="UTF-8" ?>
 2 DOCTYPE mapper
 3         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 
 6 
 9 
10 
11 <mapper namespace="cn.grandage.mapper.StudentMapper">
12     <select id="findStudentById" parameterType="int" resultType="student">
13         select * from student where id=#{id}
14     select>
15 mapper>
Student.xml

(6)、新建mybatis核心配置文件

 1 xml version="1.0" encoding="UTF-8" ?>
 2 DOCTYPE configuration
 3         PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4         "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 <configuration>
 6     
 7 
 8     
 9    <typeAliases>
10        <typeAlias type="cn.grandage.pojo.Student" alias="student"/>
11    typeAliases>
12 
13     
14     <mappers>
15         <mapper resource="Student.xml"/>
16     mappers>
17 
18 configuration>
SqlMapConfig.xml

(7)、新建spring核心配置文件,并整合mybatis:

 1 xml version="1.0" encoding="UTF-8" ?>
 2 DOCTYPE configuration
 3         PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4         "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 <configuration>
 6     
 7 
 8     
 9    <typeAliases>
10        <typeAlias type="cn.grandage.pojo.Student" alias="student"/>
11    typeAliases>
12 
13     
14     <mappers>
15         <mapper resource="Student.xml"/>
16     mappers>
17 
18 configuration>
applicationContext.xml

(8)、测试类编写:

 1 import cn.grandage.mapper.StudentMapper;
 2 import cn.grandage.pojo.Student;
 3 import org.junit.Test;
 4 import org.springframework.context.ApplicationContext;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 
 7 public class Test01 {
 8 
 9     @Test
10     public void test01() {
11         //获取applicationContext文件并加载
12         ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
13         //获取StudentDao的bean
14         StudentMapper sd = (StudentMapper) ac.getBean("studentMapper");
15         Student s = sd.findStudentById(1);
16         System.out.println("学生姓名:" + s.getUsername());
17         System.out.println("学生密码:" + s.getPassword());
18         System.out.println("学生邮箱:" + s.getEmail());
19     }
20 }
Test01

 

测试结果:

 

你可能感兴趣的:(Java——Spring整合Mybatis(IDEA版))