之前,分享过MyBatis的基础使用方法,但是,在日常开发中,大多会和Spring整合,在这里,简单分享一下。
这是学习官方的教程总结,官方教程地址:http://mybatis.github.io/spring/getting-started.html
1. pom.xml
与Spring整合的话,首先需要的是依赖包,
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>mybatis-parent</artifactId>
<groupId>org.ygy.demo</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>mybatis-spring</artifactId>
<name>mybatis-spring</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>3.1.1.RELEASE</spring.version>
<struts.version>2.3.15.1</struts.version>
<mybatis.version>3.2.2</mybatis.version>
</properties>
<profiles>
<profile>
<id>development</id>
<properties>
<jdbc_url>jdbc:oracle:thin:@xxx</jdbc_url>
<jdbc_user>dptest</jdbc_user>
<jdbc_password>dptest</jdbc_password>
</properties>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>0.2.9</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</project>
2. 实体
在持久层操作,就离不开实体啦
package org.ygy.mybatis.spring.entity;
import lombok.Data;
@Data
public class UserEntity implements java.io.Serializable{
private static final long serialVersionUID = 8663915064096283715L;
private Integer id;
private String name;
private Integer age;
}
3. 持久层接口
以前的话,我个人大多是使用配置文件,就是Mapper文件,将SQL都写在里面,在这里的话,官方使用了注解,在这里顺道也学习一下。
package org.ygy.mybatis.spring.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.ygy.mybatis.spring.entity.UserEntity;
public interface UserMapper {
@Select("SELECT * FROM t_ygy_demo_person WHERE id = #{id}")
public UserEntity queryById(@Param("id") Integer id);
@Insert("INSERT INTO t_ygy_demo_person(id , name , age) values(#{id} , #{name} , #{age})")
public void insert(UserEntity user);
@Update("UPDATE t_ygy_demo_person set name=#{name} , age=#{age} where id=#{id}")
public void update(UserEntity user);
@Delete("DELETE FROM t_ygy_demo_person where id=#{id} ")
public void delete(@Param("id") Integer id);
@Select("SELECT *FROM t_ygy_demo_person")
public List<UserEntity> queryAll();
}
4. 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<!-- 基本属性 url、user、password -->
<property name="url" value="jdbc:oracle:thin:@192.168.17.254:1521:dpweb" />
<property name="username" value="dptest" />
<property name="password" value="dptest" />
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="1" />
<property name="minIdle" value="1" />
<property name="maxActive" value="20" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="60000" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="validationQuery" value="SELECT 'x'" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize"
value="20" />
<!-- 配置监控统计拦截的filters -->
<property name="filters" value="stat" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 1.使用注解 -->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="org.ygy.mybatis.spring.mapper.UserMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
</beans>
整合的话,重点在配置文件中。
5. 测试
package org.ygy.mybatis.test;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.ygy.mybatis.spring.entity.UserEntity;
import org.ygy.mybatis.spring.mapper.UserMapper;
public class Client {
private UserMapper userMapper = null;
@Before
public void before() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
userMapper = applicationContext.getBean("userMapper" , UserMapper.class);
}
@Test
public void testInsert() {
UserEntity user = new UserEntity();
user.setId(9001);
user.setName("一户");
user.setAge(22);
userMapper.insert(user);
}
@Test
public void testDelete() {
userMapper.delete(9001);
}
@Test
public void testUpdate() {
UserEntity user = new UserEntity();
user.setId(9002);
user.setName("孙悟空");
user.setAge(222);
userMapper.insert(user);
UserEntity target = userMapper.queryById(9002);
System.out.println(target);
target.setName("猪八戒");
userMapper.update(target);
UserEntity result = userMapper.queryById(9002);
System.out.println(result);
}
@Test
public void testQueryAll() {
List<UserEntity> userList = userMapper.queryAll();
System.out.println(userList);
}
@Test
public void testQueryById() {
UserEntity user = userMapper.queryById(9002);
System.out.println(user);
}
}