Mapper代理的开发规范

1、 mapper接口的全限定名要和mapper映射文件的namespace值一致。

2、 mapper接口的方法名称要和mapper映射文件的statement的id一致。

3、 mapper接口的方法参数类型要和mapper映射文件的statement的parameterType的值一致,而且它的参数是一个。

4、 mapper接口的方法返回值类型要和mapper映射文件的statement的resultType的值一致。

代码实战

1.搭建如下工程

 2.mapper接口——interface UserMapper

package com.mf.mybatis.mapper;

import java.util.List;

import com.mf.mybatis.po.User;
import com.mf.mybatis.po.UserQueryVO; public interface UserMapper { public User findUserById(int id) throws Exception; public void insertUser(User user) throws Exception; public List<User> findUserList(UserQueryVO vo); public int findUserCount(UserQueryVO vo); public User findUserRstMap(int id);
}

3.mapper映射文件——SqlMapConfig.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> <properties resource="db.properties">
<property name="db.username" value="123" />
</properties> <typeAliases>
<typeAlias type="com.mf.mybatis.po.User" alias="user"/>
<package name="com.mf.mybatis.po" />
</typeAliases> <environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${db.driver}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
</dataSource>
</environment>
</environments> <mappers>
<!--<mapper resource="mapper/UserMapper.xml" /> -->
<package name="com.mf.mybatis.mapper" />
</mappers> </configuration>

4.Db.properties

db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8
db.username=root
db.password=root

5.log4j.properties

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

6.UserMapper.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="com.mf.mybatis.mapper.UserMapper"> <select id="findUserById" parameterType="int" resultType="User">
SELECT * FROM USER WHERE id =#{id}
</select> <insert id="insertUser" parameterType="com.mf.mybatis.po.User">
<selectKey keyProperty="id" resultType="int" order="AFTER">
SELECT LAST_INSERT_ID()
</selectKey>
INSERT INTO USER(username,birthday,sex,address) VALUES(#{username},#{birthday},#{sex},#{address})
</insert> <sql id="whereClause">
<if test="user != null">
<if test="user.username != null and user.username != ''">
AND username LIKE '%${user.username}%'
</if>
<if test="user.sex != null and user.sex != ''">
AND sex = #{user.sex}
</if>
</if> <if test="idList != null">
AND id IN
<foreach collection="idList" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</if>
</sql> <select id="findUserList" parameterType="com.mf.mybatis.po.UserQueryVO" resultType="user">
SELECT * FROM user
<where>
<include refid="whereClause" />
</where>
</select> <select id="findUserCount" parameterType="com.mf.mybatis.po.UserQueryVO" resultType="int">
SELECT count(*) FROM user
<where>
<include refid="whereClause" />
</where>
</select> <resultMap type="user" id="UserRstMap">
<id column="id_" property="id" />
<result column="username_" property="username" />
<result column="sex_" property="sex" />
</resultMap> <select id="findUserRstMap" parameterType="int" resultMap="UserRstMap">
Select id id_,username username_,sex sex_ from user where id = #{id}
</select>
</mapper>

7.UserQueryVO.java

package com.mf.mybatis.po;

import java.util.List;

public class UserQueryVO {

    private User user;

    private List<Integer> idList;

    public List<Integer> getIdList() {
return idList;
} public void setIdList(List<Integer> idList) {
this.idList = idList;
} public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
}
}

8.UserMapperTest.java

package com.mf.mybatis.mapper;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test; import com.mf.mybatis.po.User;
import com.mf.mybatis.po.UserQueryVO; public class UserMapperTest { private SqlSessionFactory sqlSessionFactory; @Before
public void setUp() throws Exception {
String resource = "SqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} @Test
public void testFindUserById() throws Exception { SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.findUserById(1);
System.out.println(user);
sqlSession.close(); } @Test
public void testInsertUser() throws Exception { SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = new User();
user.setUsername("sgz");
user.setAddress("北京");
mapper.insertUser(user);
System.out.println(user.getId());
sqlSession.commit();
sqlSession.close(); } @Test
public void testFindUserList() throws Exception { SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
UserQueryVO vo = new UserQueryVO();
// User user= new User();
// user.setUsername("沐风");
// user.setSex("1");
// vo.setUser(user);
List<Integer> idList = new ArrayList<>();
idList.add(1);
idList.add(2);
idList.add(10);
vo.setIdList(idList); List<User> list = mapper.findUserList(vo);
int count = mapper.findUserCount(vo); System.out.println(list);
System.out.println(count);
sqlSession.close();
} @Test
public void testFindUserRstMap() throws Exception { SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class); User user = mapper.findUserRstMap(1);
System.out.println(user);
sqlSession.close();
} }

 

Java SSM框架之MyBatis3(二)MyBatis之Mapper代理的开发方式的更多相关文章

  1. Java SSM框架之MyBatis3(一)MyBatis入门

    MyBatis3介绍 mybatis就是一个封装来jdbc的持久层框架,它和hibernate都属于ORM框架,但是具体的说,hibernate是一个完全的orm框架,而mybatis是一个不完全的o ...

  2. Java SSM框架之MyBatis3(八)MyBatis之动态SQL

    前言: mybatis框架中最具特色的便是sql语句中的自定义,而动态sql的使用又使整个框架更加灵活. 创建User表 /*Table structure for table `user` */ D ...

  3. Java SSM框架之MyBatis3(七)MyBatis之参数取值

    在mybatis中,参数取值方式有两种:#{ } 和 ${ } 一.#{ } select * from student where name=#{name} 编译后执行的sql语句: select ...

  4. Java SSM框架之MyBatis3(六)MyBatis之参数传递

    一.单个参数  StudentParamsMapper package cn.cnki.ref.mapper; import cn.cnki.ref.pojo.Student; public inte ...

  5. Java SSM框架之MyBatis3(四)MyBatis之一对一、一对多、多对多

    项目搭建Springboot 1.5  pom.xml <?xml version="1.0" encoding="UTF-8"?> <pro ...

  6. Java SSM框架之MyBatis3(五)MyBatis之ResultMap详解

    resultMap是Mybatis最强大的元素,它可以将查询到的复杂数据(比如查询到几个表中数据)映射到一个结果集当中. resultMap包含的元素: <!--column不做限制,可以为任意 ...

  7. Java SSM框架之MyBatis3(三)Mybatis分页插件PageHelper

    引言 对于使用Mybatis时,最头痛的就是写分页,需要先写一个查询count的select语句,然后再写一个真正分页查询的语句,当查询条件多了之后,会发现真不想花双倍的时间写count和select ...

  8. Java SSM框架之MyBatis3(十)MyBatis批量插入数据(MySql)

    插入成功后返回自增主键 <insert id="insertRole" parameterType="role" useGeneratedKeys=&qu ...

  9. java ssm框架入门(二)添加语言滤器

    使用过滤器是在web.xml中使用filter,以下是码过滤器,过滤所有资源的使用 web.xml <filter> <filter-name>setCharactor< ...

随机推荐

  1. Python读取ini配置文件封装方法

    读取配置文件 ----rw_ini.py from configparser import ConfigParser def read_config(config_file_path:str): &q ...

  2. 【Unity Shader】从NDC(归一化的设备坐标)坐标转换到世界坐标的数学原理

    从NDC(归一化的设备坐标)坐标转换到世界坐标要点 参考资料 How to go from device coordinates back to worldspace http://feepingcr ...

  3. PAT甲题题解-1032. Sharing (25)-链表水题

    #include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...

  4. PAT甲题题解-1037. Magic Coupon (25)-贪心,水

    题目说了那么多,就是给你两个序列,分别选取元素进行一对一相乘,求得到的最大乘积. 将两个序列的正和负数分开,排个序,然后分别将正1和正2前面的相乘,负1和负2前面的相乘,累加和即可. #include ...

  5. Scalable Object Detection using Deep Neural Networks译文

    原文:https://arxiv.org/abs/1312.2249

  6. MongoDB给数据库创建用户

    一.先以非授权的模式启动MongoDB 非授权: linux/Mac : mongod -f /mongodb/etc/mongo.conf windows  : mongod --config c: ...

  7. Git使用:安装,使用及常用命令整理

    对于程序猿而言,git是最常接触的工具之一,因此需要熟练快速掌握其技巧. git安装: windwos:  [原创]Windows平台下Git的安装与配置 Ubuntu:git与github在ubun ...

  8. 如何取消mysql的密码?

    有两种方法: 1.mysql命令 SET PASSWORD FOR root@localhost=PASSWORD(''); 2.运行  mysqladmin 命令 mysqladmin -u roo ...

  9. vue中的minix

    minix 是个什么东西, 就是混合,把你混合给我 浅显表述就是 你说 : ‘我叫李四’, 我说 : ‘我叫张三’, 然后把你 混合给我, 就成了 我说 : ‘我叫张三我叫李四’, 所有解说都在例子里 ...

  10. django rest framework serializers小结

    注:转载至https://blog.csdn.net/l_vip/article/details/79156113 引言 serializers是什么?官网是这样的”Serializers allow ...