本文适用于初学者:

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

完整的工程目录如下:

整合思路:

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

下面开始工程搭建:

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

点击Finish完成

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

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

第二步:项目准备

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

 CREATE DATABASE /*!32312 IF NOT EXISTS*/`test` /*!40100 DEFAULT CHARACTER SET utf8 */;

 USE `test`;

 /*Table structure for table `student` */

 DROP TABLE IF EXISTS `student`;

 CREATE TABLE `student` (
`id` int(11) DEFAULT NULL,
`username` varchar(20) DEFAULT NULL,
`password` varchar(20) DEFAULT NULL,
`email` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*Data for the table `student` */ insert into `student`(`id`,`username`,`password`,`email`) values
(1,'jack','','123@jack'),
(2,'rose','','rose@123');

sql语句

第三步:正式开发

(1)、修改pom文件

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>cn.grandage</groupId>
<artifactId>SpringMybatisTest</artifactId>
<version>1.0-SNAPSHOT</version> <properties>
<!-- spring版本号 -->
<spring.version>4.1.6.RELEASE</spring.version>
<!-- mybatis版本号 -->
<mybatis.version>3.2.6</mybatis.version>
<!-- log4j日志文件管理包版本 -->
<slf4j.version>1.7.7</slf4j.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<!-- 表示开发的时候引入,发布的时候不会加载此包 -->
<scope>test</scope>
</dependency>
<!-- spring核心包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- mybatis核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- mybatis/spring包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- 导入java ee jar 包 -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<!-- 导入Mysql数据库链接jar包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<!-- c3p0连接池jar -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<!-- JSTL标签类 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- 日志文件管理包 -->
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- 格式化对象,方便输出日志 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.41</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- log end -->
<!-- 映入JSON -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<!-- 上传组件包 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
</dependencies> </project>

POM文件

(2)、新建实体类Student:

 package cn.grandage.pojo;

 public class Student {

     private Integer id;
private String username;
private String password;
private String email; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
} @Override
public String toString() {
return "StudentMapper{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", email='" + email + '\'' +
'}';
}
}

Student

(3)、新建db.properties文件

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

db.properties

(4)、新建StudentMapper接口

 package cn.grandage.mapper;

 import cn.grandage.pojo.Student;

 public interface StudentMapper {

     public Student findStudentById(int id);
}

StudentMapper

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

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace:命名空间,对sql进行一个分类管理 -->
<!-- 注意:namespace在mapper代理时,具有重要且特殊的作用
对应mapper接口的全限定名
--> <!--mybatis映射配置文件-->
<mapper namespace="cn.grandage.mapper.StudentMapper">
<select id="findStudentById" parameterType="int" resultType="student">
select * from student where id=#{id}
</select>
</mapper>

Student.xml

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

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--mybatis核心配置文件--> <!--给类取别名,简化输入,方便映射配置文件中使用-->
<typeAliases>
<typeAlias type="cn.grandage.pojo.Student" alias="student"/>
</typeAliases> <!--加载mapper映射配置文件-->
<mappers>
<mapper resource="Student.xml"/>
</mappers> </configuration>

SqlMapConfig.xml

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

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--mybatis核心配置文件--> <!--给类取别名,简化输入,方便映射配置文件中使用-->
<typeAliases>
<typeAlias type="cn.grandage.pojo.Student" alias="student"/>
</typeAliases> <!--加载mapper映射配置文件-->
<mappers>
<mapper resource="Student.xml"/>
</mappers> </configuration>

applicationContext.xml

(8)、测试类编写:

 import cn.grandage.mapper.StudentMapper;
import cn.grandage.pojo.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test01 { @Test
public void test01() {
//获取applicationContext文件并加载
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取StudentDao的bean
StudentMapper sd = (StudentMapper) ac.getBean("studentMapper");
Student s = sd.findStudentById(1);
System.out.println("学生姓名:" + s.getUsername());
System.out.println("学生密码:" + s.getPassword());
System.out.println("学生邮箱:" + s.getEmail());
}
}

Test01

测试结果:

最新文章

  1. MongoDB aggregate 运用篇
  2. 【Beta】第四次任务发布
  3. javascript基础05
  4. Android 保存图片到SQLite
  5. BLE教程 - 官方tutorial翻译
  6. URAL 2080 Wallet 莫队算法
  7. 巧用Javascript中的slice()
  8. DedeCMS全版本通杀SQL注入漏洞利用代码
  9. scala的传名参数
  10. 两种常用的MySQL官方客户端软件
  11. 身份证校验程序(上)- 零基础入门学习Delphi48
  12. HTML中IE条件注释判断语句(&lt;!--[if XX IE X]&gt;&lt;![endif]--&gt;)
  13. 为什么Lisp没有流行起来
  14. metrics实践 (metrics-spring)
  15. 数数字(Digit Counting,ACM/ICPC Danang 2007,UVa1225)
  16. 初识NumPy库-基本操作
  17. ncnn编译安装
  18. rem、em、px、pt及网站字体大小设配
  19. 20155233 刘高乐 Exp9 Web安全基础
  20. hdu 4240 最大流量路径

热门文章

  1. python3(二)
  2. 刨根问底系列(1)——虚假唤醒(spurious wakeups)的原因以及在pthread_cond_wait、pthread_cond_singal中使用while的必要性
  3. 一个有关 scala 编程语言 的博客
  4. 10个步骤教你如何安装Anaconda安装,Python数据分析入门必看
  5. lua使用笔记1:Linux 中安装lua
  6. Redis 的 maxmemory 和 dbnum 默认值都是多少?对于最大值会有限制吗?
  7. 关于phpstorm、idea、gogland等等ide全家桶设置
  8. thinkphp--多表查询
  9. php 全局变量和超全局变量
  10. 2019-2020-1 20199310《Linux内核原理与分析》第四周作业