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.0</modelVersion> 6 7 <groupId>cn.grandage</groupId> 8 <artifactId>SpringMybatisTest</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <properties> 12 <!-- spring版本号 --> 13 <spring.version>4.1.6.RELEASE</spring.version> 14 <!-- mybatis版本号 --> 15 <mybatis.version>3.2.6</mybatis.version> 16 <!-- log4j日志文件管理包版本 --> 17 <slf4j.version>1.7.7</slf4j.version> 18 <log4j.version>1.2.17</log4j.version> 19 </properties> 20 <dependencies> 21 <dependency> 22 <groupId>junit</groupId> 23 <artifactId>junit</artifactId> 24 <version>4.11</version> 25 <!-- 表示开发的时候引入,发布的时候不会加载此包 --> 26 <scope>test</scope> 27 </dependency> 28 <!-- spring核心包 --> 29 <dependency> 30 <groupId>org.springframework</groupId> 31 <artifactId>spring-core</artifactId> 32 <version>${spring.version}</version> 33 </dependency> 34 <dependency> 35 <groupId>org.springframework</groupId> 36 <artifactId>spring-web</artifactId> 37 <version>${spring.version}</version> 38 </dependency> 39 <dependency> 40 <groupId>org.springframework</groupId> 41 <artifactId>spring-oxm</artifactId> 42 <version>${spring.version}</version> 43 </dependency> 44 <dependency> 45 <groupId>org.springframework</groupId> 46 <artifactId>spring-tx</artifactId> 47 <version>${spring.version}</version> 48 </dependency> 49 <dependency> 50 <groupId>org.springframework</groupId> 51 <artifactId>spring-jdbc</artifactId> 52 <version>${spring.version}</version> 53 </dependency> 54 <dependency> 55 <groupId>org.springframework</groupId> 56 <artifactId>spring-webmvc</artifactId> 57 <version>${spring.version}</version> 58 </dependency> 59 <dependency> 60 <groupId>org.springframework</groupId> 61 <artifactId>spring-aop</artifactId> 62 <version>${spring.version}</version> 63 </dependency> 64 <dependency> 65 <groupId>org.springframework</groupId> 66 <artifactId>spring-context-support</artifactId> 67 <version>${spring.version}</version> 68 </dependency> 69 <dependency> 70 <groupId>org.springframework</groupId> 71 <artifactId>spring-test</artifactId> 72 <version>${spring.version}</version> 73 </dependency> 74 <!-- mybatis核心包 --> 75 <dependency> 76 <groupId>org.mybatis</groupId> 77 <artifactId>mybatis</artifactId> 78 <version>${mybatis.version}</version> 79 </dependency> 80 <!-- mybatis/spring包 --> 81 <dependency> 82 <groupId>org.mybatis</groupId> 83 <artifactId>mybatis-spring</artifactId> 84 <version>1.2.2</version> 85 </dependency> 86 <!-- 导入java ee jar 包 --> 87 <dependency> 88 <groupId>javax</groupId> 89 <artifactId>javaee-api</artifactId> 90 <version>7.0</version> 91 </dependency> 92 <!-- 导入Mysql数据库链接jar包 --> 93 <dependency> 94 <groupId>mysql</groupId> 95 <artifactId>mysql-connector-java</artifactId> 96 <version>5.1.39</version> 97 </dependency> 98 <!-- c3p0连接池jar --> 99 <dependency> 100 <groupId>c3p0</groupId> 101 <artifactId>c3p0</artifactId> 102 <version>0.9.1.2</version> 103 </dependency> 104 <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 --> 105 <dependency> 106 <groupId>commons-dbcp</groupId> 107 <artifactId>commons-dbcp</artifactId> 108 <version>1.2.2</version> 109 </dependency> 110 <!-- JSTL标签类 --> 111 <dependency> 112 <groupId>jstl</groupId> 113 <artifactId>jstl</artifactId> 114 <version>1.2</version> 115 </dependency> 116 <!-- 日志文件管理包 --> 117 <!-- log start --> 118 <dependency> 119 <groupId>log4j</groupId> 120 <artifactId>log4j</artifactId> 121 <version>${log4j.version}</version> 122 </dependency> 123 <!-- 格式化对象,方便输出日志 --> 124 <dependency> 125 <groupId>com.alibaba</groupId> 126 <artifactId>fastjson</artifactId> 127 <version>1.1.41</version> 128 </dependency> 129 <dependency> 130 <groupId>org.slf4j</groupId> 131 <artifactId>slf4j-api</artifactId> 132 <version>${slf4j.version}</version> 133 </dependency> 134 <dependency> 135 <groupId>org.slf4j</groupId> 136 <artifactId>slf4j-log4j12</artifactId> 137 <version>${slf4j.version}</version> 138 </dependency> 139 <!-- log end --> 140 <!-- 映入JSON --> 141 <dependency> 142 <groupId>org.codehaus.jackson</groupId> 143 <artifactId>jackson-mapper-asl</artifactId> 144 <version>1.9.13</version> 145 </dependency> 146 <!-- 上传组件包 --> 147 <dependency> 148 <groupId>commons-fileupload</groupId> 149 <artifactId>commons-fileupload</artifactId> 150 <version>1.3.1</version> 151 </dependency> 152 <dependency> 153 <groupId>commons-io</groupId> 154 <artifactId>commons-io</artifactId> 155 <version>2.4</version> 156 </dependency> 157 <dependency> 158 <groupId>commons-codec</groupId> 159 <artifactId>commons-codec</artifactId> 160 <version>1.9</version> 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 <!-- namespace:命名空间,对sql进行一个分类管理 --> 6 <!-- 注意:namespace在mapper代理时,具有重要且特殊的作用 7 对应mapper接口的全限定名 8 --> 9 10 <!--mybatis映射配置文件--> 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 <!--mybatis核心配置文件--> 7 8 <!--给类取别名,简化输入,方便映射配置文件中使用--> 9 <typeAliases> 10 <typeAlias type="cn.grandage.pojo.Student" alias="student"/> 11 </typeAliases> 12 13 <!--加载mapper映射配置文件--> 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 <!--mybatis核心配置文件--> 7 8 <!--给类取别名,简化输入,方便映射配置文件中使用--> 9 <typeAliases> 10 <typeAlias type="cn.grandage.pojo.Student" alias="student"/> 11 </typeAliases> 12 13 <!--加载mapper映射配置文件--> 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
测试结果: