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. SVN基础操作

    SVN基础操作 安装 #大多数Linux版本自带svn svn --version #如果没有安装可用yum安装 yum install subversion 生命周期 创建版本库 检出 更新 执行变 ...

  2. App推荐 | Google Tasks

    前不久,Google推出了一款移动任务管理应用Google Task,在使用2天后,写一下使用感受,并与Google同类产品Keep进行一个对比. 首先欣赏几张官方的App截图 然后来看一下官方的介绍 ...

  3. 初次接触OSSEC

    OSSEC是一款开源的系统监控平台.它集成了HIDS(主机入侵检测).日志监控.安全事件管理(SIM).安全信息和事件管理(SIEM)于一身,结构简单.功能强大的开源解决方案. 主要优点 满足合规性 ...

  4. sass和less的几点不同

    1.申明和使用变量 sass使用$符号来标识变量(老版本的sass使用!来标识变量),比如$highlight-color和$sidebar-width. 与CSS属性不同,变量可以在css规则块定义 ...

  5. 20135327郭皓--Linux内核分析第三周 构造一个简单的Linux系统MenuOS

    Linux内核分析第三周  构造一个简单的Linux系统MenuOS 前提回顾 1.计算机是如何工作的三个法宝 1.存储程序计算机 2.函数调用堆栈 3.中断 2.操作系统的两把宝剑 中断上下文的切换 ...

  6. JAVA 操作系统已经来到第五个版本了 现陆续放出三个版本 这是第二个版本

    package System2; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import ...

  7. 使用Visual Studio 2013进行单元测试

    使用Visual Studio 2013进行单元测试 1.打开VS2013 --> 新建一个项目.这里我们默认创建一个控制台项目.取名为UnitTestDemo 2.在解决方案里面新增一个单元测 ...

  8. ElasticSearch 2 (14) - 深入搜索系列之全文搜索

    ElasticSearch 2 (14) - 深入搜索系列之全文搜索 摘要 在看过结构化搜索之后,我们看看怎样在全文字段中查找相关度最高的文档. 全文搜索两个最重要的方面是: 相关(relevance ...

  9. [转帖] Kubernetes如何使用ReplicationController、Replica Set、Deployment管理Pod ----文章很好 但是还没具体操作实践 也还没记住.

    Kubernetes如何使用ReplicationController.Replica Set.Deployment管理Pod https://blog.csdn.net/yjk13703623757 ...

  10. GS 服务器超时时间设置

    工作中 遇到一个超时的问题 与徐庆同学沟通后 了解了下超时时间设置的地方 1.web.congfig问题: 常规路径 C:\Program Files\GenerSoft\bscw_local\web ...