spring與mybatsi框架的整合
前提:
在開始使用 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>