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">
<!-- namespace命名空间,就是对sql进行分类化管理 理解sql隔离 -->
<mapper namespace="com.liu.mybatis.mapper.UserMapper">
<!-- 定义一个resultMap -->
<resultMap type="user" id="userResultMap">
<id column="id_" property="id"/>
<result column="address_" property="address"/>
</resultMap>
<!-- 定义sql片段 -->
<sql id="user_query_where">
<if test="userCustom!=null">
<if test="userCustom.sex!=null and userCustom.sex!=''">
and user.sex=#{userCustom.sex}
</if>
<if test="userCustom.username!=null and userCustom.username!=''">
and user.username like '%${userCustom.username}%'
</if>
</if>
</sql>
<!-- 在映射文件中配置sql语句 -->
<!-- 通过ID查询sql记录 -->
<!-- 通过select来执行查询
id: 标识映射文件中的sql 称为statement的id
将sql语句封装到mappedStatement中
parameterType:指定输入参数的类型 ,这里指定int型
#{}表示一个占位符
#{id} 表示接收输入的参数 id就是输入的参数名称
resultTypeL: 指定sql输出结果的javadui类型
-->
<select id="findUserById" parameterType="int" resultType="com.liu.mybatis.po.User">
SELECT * FROM USER WHERE id = #{id}
</select>
<!-- 使用resultMap输出映射 -->
<select id="findUserByIdResultMap" parameterType="int" resultMap="userResultMap">
SELECT id id_,address address_ FROM USER WHERE id = #{id}
</select>
<!-- ${}是一个拼接符号 -->
<select id="findUserByName" parameterType="java.lang.String" resultType="user">
SELECT * FROM USER WHERE username like '%${value}%'
</select>
<!-- 用户信息综合查询 -->
<select id="findUserList" parameterType="com.liu.mybatis.po.UserQueryVo"
resultType="com.liu.mybatis.po.UserCustom">
select * from user
<where>
<if test="userCustom!=null">
<if test="userCustom.sex!=null and userCustom.sex!=''">
and user.sex=#{userCustom.sex}
</if>
<if test="userCustom.username!=null and userCustom.username!=''">
and user.username like '%${userCustom.username}%'
</if>
</if>
<if test="ids!=null">
<!-- foreach实现sql的拼接 and id=1 or id=3 or id=5 -->
<foreach collection="ids" item="user_id" open="AND (" close=")" separator="or">
id=#{user_id}
</foreach>
<!-- foreach实现sql的拼接 and id in (1,3,5) -->
<!-- <foreach collection="ids" item="user_id" open="and id in (" close=")" separater=",">
#{user_id}
</foreach> -->
</if>
</where>
</select>
<!-- 用户信息总数查询 -->
<select id="findUserCount" parameterType="com.liu.mybatis.po.UserQueryVo" resultType="int">
select * from user
<where>
<include refid="user_query_where"></include>
</where>
</select>
<!-- selectkey获取自增主键 -->
<insert id="insertUser" parameterType="com.liu.mybatis.po.User">
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>
insert into user(username,birthday,sex,address) value(#{username},#{birthday},#{sex},#{address})
</insert>
<delete id="deleteUser" parameterType="java.lang.Integer">
delete from user where id = #{id}
</delete>
<update id="updateUser" parameterType="com.liu.mybatis.po.User">
update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id}
</update> </mapper>

UserMapper.java

package com.liu.mybatis.mapper;

import java.util.List;

import com.liu.mybatis.po.User;
import com.liu.mybatis.po.UserCustom;
import com.liu.mybatis.po.UserQueryVo; public interface UserMapper {
public List<UserCustom> findUserList(UserQueryVo userQueryVo) throws Exception; public int findUserCount(UserQueryVo userQueryVo) throws Exception; public User findUserById(int id) throws Exception; public User findUserByIdResultMap(int id) throws Exception; public List<User> findUserByName(String username) throws Exception; public void insertUser(User user) throws Exception; public void deleteUser(int id) throws Exception; }

UserQueryVo.java

package com.liu.mybatis.po;

import java.util.List;

/*
* 包装类型
*/
public class UserQueryVo {
private UserCustom userCustom; private List<Integer> ids; public UserCustom getUserCustom() {
return userCustom;
} public void setUserCustom(UserCustom userCustom) {
this.userCustom = userCustom;
} public List<Integer> getIds() {
return ids;
} public void setIds(List<Integer> ids) {
this.ids = ids;
} }

UserCustom.java

package com.liu.mybatis.po;

/*
* 用户的扩展类
*/
public class UserCustom extends User{ }

测试程序

package com.liu.mybatis.mapper;

import static org.junit.Assert.*;

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.liu.mybatis.po.User;
import com.liu.mybatis.po.UserCustom;
import com.liu.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对象,mybatis自动生成mapper代理对象
UserMapper userMapper = sqlSession.getMapper(UserMapper.class); System.out.println(UserMapper.class); User user = userMapper.findUserById(28); System.out.println("20170902 =" + user);
} @Test
public void testFindUserByName() throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession(); //创建UserMapper对象,mybatis自动生成mapper代理对象
UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<User> list = userMapper.findUserByName("晓春"); System.out.println("20170902 =" + list);
}
//用户综合信息查询
@Test
public void testFindUserList() throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession(); //创建UserMapper对象,mybatis自动生成mapper代理对象
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
System.out.println("11111111111111111===================");
UserQueryVo userQueryVo =new UserQueryVo(); UserCustom userCustom = new UserCustom();
userCustom.setUsername("王晓春");
//userCustom.setSex("1");
List<Integer> ids= new ArrayList<Integer>();
ids.add(1);
ids.add(28);
ids.add(30);
userQueryVo.setIds(ids);
userQueryVo.setUserCustom(userCustom); List<UserCustom> list = userMapper.findUserList(userQueryVo); System.out.println("20170903 10:15 =" + list);
} @Test
public void testFindUserCount() throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession(); //创建UserMapper对象,mybatis自动生成mapper代理对象
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
UserQueryVo userQueryVo =new UserQueryVo(); UserCustom userCustom = new UserCustom();
userCustom.setUsername("王晓春");
userCustom.setSex("1");
userQueryVo.setUserCustom(userCustom); int count = userMapper.findUserCount(userQueryVo); System.out.println("20170902 23:15 =" + count);
} @Test
public void testFindUserByIdResultMap() throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession(); //创建UserMapper对象,mybatis自动生成mapper代理对象
UserMapper userMapper = sqlSession.getMapper(UserMapper.class); User user = userMapper.findUserByIdResultMap(28); System.out.println("20170902 ===" + user);
}
}

mybatis基础之二的更多相关文章

  1. Mybatis基础学习(二)—开发Dao方式

    一.原始Dao开发方式 UserDao.java public interface UserDao{ public User findUserByID(Serializable id); public ...

  2. JAVA之Mybatis基础入门二 -- 新增、更新、删除

    上一节说了Mybatis的框架搭建和简单查询,这次我们来说一说用Mybatis进行基本的增删改操作: 一. 插入一条数据 1.首先编写USER.XML(表的xml)使用insert元素,元素写在map ...

  3. Mybatis基础(二)

    Mybatis连接池 Mybatis连接池提供了三种配置方式,配置的位置在SqlMapConfig.xml的dataSource标签中,其type属性就是配置连接池的种类.type的可取值 1.POO ...

  4. MyBatis基础入门《二十》动态SQL(foreach)

    MyBatis基础入门<二十>动态SQL(foreach) 1. 迭代一个集合,通常用于in条件 2. 属性 > item > index > collection : ...

  5. MyBatis基础入门《十二》删除数据 - @Param参数

    MyBatis基础入门<十二>删除数据 - @Param参数 描述: 删除数据,这里使用了@Param这个注解,其实在代码中,不使用这个注解也可以的.只是为了学习这个@Param注解,为此 ...

  6. MyBatis基础入门《二》Select查询

    MyBatis基础入门<二>Select查询 使用MySQL数据库,创建表: SET NAMES utf8mb4; ; -- ---------------------------- -- ...

  7. mybatis基础系列(二)——基础语法、别名、输入映射、输出映射

    增删改查 mapper根节点及其子节点 mybatis框架需要读取映射文件创建会话工厂,映射文件是以<mapper>作为根节点,在根节点中支持9个元素,分别为insert.update.d ...

  8. myBatis 基础测试 表关联关系配置 集合 测试

    myBatis 基础测试 表关联关系配置 集合 测试 测试myelipse项目源码 sql 下载 http://download.csdn.net/detail/liangrui1988/599388 ...

  9. mybatis入门系列二之输入与输出参数

    mybatis入门系列二之详解输入与输出参数   基础知识   mybatis规定mapp.xml中每一个SQL语句形式上只能有一个@parameterType和一个@resultType 1. 返回 ...

随机推荐

  1. 前端PHP入门-011-可变函数

    可变函数,我们也会称呼为变量函数.简单回顾一下之前的知识点: <?php $hello = 'world'; $world = '你好'; //输出的结果为:你好 echo $$hello; ? ...

  2. [LeetCode] 26. Remove Duplicates from Sorted Array ☆

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  3. POJ 1113 Wall 凸包 裸

    LINK 题意:给出一个简单几何,问与其边距离长为L的几何图形的周长. 思路:求一个几何图形的最小外接几何,就是求凸包,距离为L相当于再多增加上一个圆的周长(因为只有四个角).看了黑书使用graham ...

  4. select表单元素详解及下拉列表模拟实现

    原文地址:→看过来 写在前面 select 是HTML表单元素中很常用的一个,其中很重要的几个属性常被忽略,但这几个属性却能帮助我们完成很多的功能,当然,select下拉列表默认样式很不友好,所以更多 ...

  5. Linux 安装tomcat,搭建web app运行环境

    Tomcat 8 下载地址:https://tomcat.apache.org/download-80.cgi 解压tomcat:tar -xf apache-tomcat-8.5.31.tar.gz ...

  6. 小程序 mcrypt加密拓展在php7.1 废弃 使用openssl替代方案

    原加密方法 使用mcrypt //获得16位随机字符串,填充到明文之前 $random = $this->getRandomStr(); $text = $random . pack(" ...

  7. ASP.NET EF 使用LinqPad 快速学习Linq

    使用LinqPad这个工具可以很快学习并掌握linq[Language Integrated Query] linqPad官方下载地址:http://www.linqpad.net/ linqPad4 ...

  8. 图片轮播器——jquery插件

    下载:http://files.cnblogs.com/files/wordblog/jiaoben828.rar

  9. Callback2.0

    Callback定义? a callback is a piece of executable code that is passed as an argument to other code, wh ...

  10. cin循环输入控制问题

    之前写一个简单的输入节点值自动生成链表的测试程序,发现cin的输入控制好像在VC++6.0和VS2010中不一样,特此记录. 现在有以下代码: vector<int> ivec; int ...