前提:
在开始使用 MyBatis-Spring 的整合之前,很重要的一点是,你要熟悉 Spring 和 MyBatis 这两个框架还有和它们有关的术语。
MyBatis-Spring要求Java5及以上版本还有下面列出的MyBatis和Spring版本:
MyBatis-Spring | MyBatis | Spring |
---|---|---|
1.0.0 或 1.0.1 | 3.0.1 到 3.0.5 | 3.0.0 或以上 |
1.0.2 | 3.0.6 | 3.0.0 或以上 |
1.1.0 | 3.1.0 或以上 | 3.0.0 或以上 |
我这里使用mybatis 3.4.6与spring5.0.0
第一步:
下载jar包:
大家如果觉得有些麻烦,我已经整合好了整个项目的jar包: https://pan.baidu.com/s/1I-IuVMoWdm-y0If8Ji3KSw 密码: rzjd
spring的jar下载方式教程:点击这里
mybatis的jar下载链接:点击这里
mybatis与spring的整合jar包下载:点击这里
最后的数据库链接jar:mysql-connector-java-5.1.45-bin.jar
第二步:创建一个web项目
src: | 该文件夹下存放源码 |
confing(建立Source Folder文件) | 该文件夹下存放配置文件, |
src下建立MVC视图:
domain:Employee(Bean)
dao:
EmployeeDao(接口)------------>public Employee getEmployeeById(Integer id)
EmployeeDaoImp(EmployeeDao的实现类)------------>public Employee getEmployeeById(Integer id)
service:
EmployeeService:
需要加载Spring的配置文件:
applicationContext = new ClassPathXmlApplicationContext("classpath:spring/ApplicationContext.xml");
(web层这里没有写)
config下建立相关配置文件:
dpconfig.properties-------->该文件中存放的是连接数据库的信息
log4j.properties:配置输出日志的文件
log4j2.xml:配置输出日志的文件
mybatis:
(mybatis的总配置文件(可以添加其它映射文件的的))
mybatis-config.xml
mybatisMapping:
(mybtais的映射文件)
EmployeeMapping.xml
spring:
(spring的配置文件)
ApplicationContext.xml
最后的结果图层:
第三步:
在第一步和第二步我们已近完成了,jar包的导入以及层次结构的创建,接下来,我们就需要完成代码的思路部分了!
其中整合中最重要的就是要知道Dao层需要继承SqlSessionSupport类后通过getSession的方法获得SqlSession对象,而需要获得该对象,又必须给SqlSessionSupport其中的属性SqlSessionFactory赋值,给SqlSessionFactory赋值又需要给出configLocation(mybatis的总配置文件)和dataSource(数据源),最后再把利用dbconfig.properties将数据源配置好,就可以一层一层的赋值了!!!!
第四步:
domain层:
Employee.java
package cn.gxm.domain;
public class Employee {
private Integer id;
private String lastname;
private double salary;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee [id=" + id + ", lastname=" + lastname + ", salary=" + salary + "]";
}
public Employee(String lastname, double salary) {
super();
this.lastname = lastname;
this.salary = salary;
}
public Employee() {
}
}
dao层代码:
employeeDao.ava
package cn.gxm.dao;
import cn.gxm.domain.Employee;
public interface EmployeeDao {
public Employee getEmployeeById(Integer id);
}
employeeDaoImp.java
package cn.gxm.dao;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import cn.gxm.domain.Employee;
public class EmployeeDaoImp extends SqlSessionDaoSupport implements EmployeeDao{
@Override
public Employee getEmployeeById(Integer id) {
SqlSession sqlSession = this.getSqlSession();
Employee emp =sqlSession.selectOne("test.getEmployeeById", 12);
return emp;
}
}
service层:
package cn.gxm.service;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.gxm.dao.EmployeeDao;
import cn.gxm.domain.Employee;
public class EmployeeService {
private ApplicationContext applicationContext;
@Before
public void load() {
applicationContext = new ClassPathXmlApplicationContext("classpath:spring/ApplicationContext.xml");
}
@Test
public void getEmployeeById() {
EmployeeDao empDao = (EmployeeDao) applicationContext.getBean("employeeDaoImp");
Employee emp = empDao.getEmployeeById(12);
System.out.println(emp.toString());
}
}
mybatisMapping/EmployeeMapping.xml
<?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">
<mapper namespace="test">
<select id="getEmployeeById" resultType="cn.gxm.domain.Employee">
select * from employee where id=#{id}
</select>
</mapper>
mybatis/mybatis-config.xml
<?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>
<!-- 引入employee的映射文件 -->
<mappers>
<mapper resource="mybatisMapping/EmployeeMapper.xml"/>
</mappers>
</configuration>
spring.ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd ">
<!-- 加载dbconfig.properties配置文件,因为我这里的config文件是源文件 -->
<context:property-placeholder location="classpath:dbconfig.properties"/>
<!-- 数据源使用DBCP -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="mybatis/mybatis-config.xml"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="employeeDaoImp" class="cn.gxm.dao.EmployeeDaoImp">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
dbconfig.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?useSSL=false
jdbc.username=root
jdbc.password=123456
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%m%n" />
</Console>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>