mybatis整合Mapper通用工具,实现单表基本操作
2015-09-25 14:17
931 查看
木秀于林,风必摧之
文档资料: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还是查找文档······
相关文章推荐
- 菜鸟成长记(1)——cocos2dx-2048小游戏实战之界面布局
- 初学android 运行模拟器
- 全面解析Unity3D自动生成的脚本工程文件
- UT4418开发板启动模式设置分析
- Cocos绑定Lua自定义类——Mac
- Android 反编绎工具JEB简介及下载
- Android Silent Install Apk(Android 静默安装)
- iOS 8 AutoLayout与Size Class自悟
- [Cocos2d-x 升级IOS9错误解决方案]Unexpected CFBundleExecutable Key.The bundle atXXXX
- cocos2dx ui button 添加调用事件
- iOS开发日记25-CALayer动画之进度条
- Xcode 升级后,常常遇到的遇到的警告、错误,解决方法
- ios蓝牙开发(三)app作为外设被连接的实现
- 关于iOS9系统对App应用的几个修改
- iOS 蓝牙开发(二)iOS 连接外设的代码实现
- 关于iOS百度地图标注
- iOS蓝牙开发(一)蓝牙相关基础知识
- [Cocos2d-x 升级IOS9错误解决方案]Invalid Bundle.iPad Multitasking support requires these orientations:XXX
- 为什么放弃WebView 而 使用 crosswalk!
- Android加载不同DPI资源与内存消耗之间的关系