木秀于林,风必摧之
文档资料:https://github.com/abel533/Mapper
需要导入的maven依赖文件如下:
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>
* 第二个是用于model注解的使用。
mybatis.xml配置文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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/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">
<!-- 配置MyBatis SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:myBatis-config.xml"></property>
<property name="mapperLocations"
value="classpath*:com/citic/gatz/mapping/*Mapper.xml"></property>
</bean>
<!-- 采用自动扫描包的形式来实例化dao层 -->
<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.citic.gatz.dao.**" />
<property name="properties">
<value>
mappers=tk.mybatis.mapper.common.Mapper
</value>
</property>
</bean>
</beans>
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>
<settings>
<setting name="cacheEnabled" value="false" />
<setting name="useGeneratedKeys" value="true" />
<setting name="defaultExecutorType" value="REUSE" />
</settings>
<!-- 别名配置 -->
<typeAliases>
<package name="com.citic.gatz.model" />
</typeAliases>
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="mysql" />
</plugin>
</plugins>
</configuration>
IBaseDao:
package com.citic.gatz.base;
import tk.mybatis.mapper.common.BaseMapper;
/**
*
* @author fengchao
* Mapper中通用的方法:
* 继承了base组合接口中的4个组合接口,包含完整的CRUD方法
*
* @param <T>
*/
public interface IBaseMapper<T> extends BaseMapper<T> {
}
以UserTest为例来说明:
sql:
CREATE TABLE `user_test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(32) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=78 DEFAULT CHARSET=utf8;
model:
package com.citic.gatz.model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* model层测试类
* @author fengchao
*
*/
@Table(name="user_test")
public class UserTest implements Serializable {
private static final long serialVersionUID = 6626527450093471276L;
@Id
@Column(name = "id")
private Integer id;
private String username;
private Integer age;
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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
*若有的属性不需要和数据库挂钩,在使用@Transient进行注解标示
dao:
package com.citic.gatz.dao;
import java.util.List;
import com.citic.gatz.base.IBaseMapper;
import com.citic.gatz.model.UserTest;
public interface UserTestMapper extends IBaseMapper<UserTest> {
/**
* 根据username查找对应的信息
* @param username
* @return
*/
List<UserTest> selectByUsername(String username);
}
*其中里面有一个特点的业务的方法selectByUsername,只时候需要自己在Mapper文件里面完善。
mapper:
<?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="com.citic.gatz.dao.UserTestMapper">
<resultMap id="BaseResultMap" type="userTest">
<id column="id" property="id" jdbcType="INTEGER" />
<result column="username" property="username" jdbcType="VARCHAR" />
<result column="age" property="age" jdbcType="INTEGER" />
</resultMap>
<sql id="Base_Column_List" >
id,username,age
</sql>
<!-- 根据用户名查找 -->
<select id="selectByUsername" parameterType="java.lang.String" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from
user_test ut
where
ut.username = #{username}
</select>
</mapper>
测试类如下:
package com.dao.test;
import java.util.List;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.citic.gatz.dao.UserTestMapper;
import com.citic.gatz.model.UserTest;
import com.github.pagehelper.PageHelper;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations ={"classpath:beans.xml"})
public class UserTestDaoTest {
@Autowired
private UserTestMapper userTestMapper;
@Test
public void insert() {
for(int i = 0; i < 10; i++) {
UserTest obj = new UserTest();
obj.setUsername("feng" + i);
obj.setAge(24 + i);
int insert = userTestMapper.insert(obj);
System.out.println(insert);
}
}
@Test
public void delete() {
int key = userTestMapper.deleteByPrimaryKey(8);
System.out.println(key);
}
@Test
public void update() {
UserTest userTest = userTestMapper.selectByPrimaryKey(7);
userTest.setUsername("saaa");
//根据主键更新实体全部字段,null值会被更新
int key = userTestMapper.updateByPrimaryKey(userTest);
System.out.println(key);
}
@Test
public void find() {
PageHelper.startPage(2, 3); //第二页 三条数据
List<UserTest> list = userTestMapper.selectAll();
for (UserTest userTest : list) {
System.out.println(userTest.getUsername());
}
}
@Test
public void findByUsername() {
List<UserTest> list = userTestMapper.selectByUsername("feng9");
for (UserTest userTest : list) {
System.out.println(userTest.getUsername());
}
}
}
除了最后一个方法是自己写的,其余的都是通过Mapper插件完成。 完毕!详细API还是查找文档······