mybatis基础之二
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基础之二的更多相关文章
- Mybatis基础学习(二)—开发Dao方式
一.原始Dao开发方式 UserDao.java public interface UserDao{ public User findUserByID(Serializable id); public ...
- JAVA之Mybatis基础入门二 -- 新增、更新、删除
上一节说了Mybatis的框架搭建和简单查询,这次我们来说一说用Mybatis进行基本的增删改操作: 一. 插入一条数据 1.首先编写USER.XML(表的xml)使用insert元素,元素写在map ...
- Mybatis基础(二)
Mybatis连接池 Mybatis连接池提供了三种配置方式,配置的位置在SqlMapConfig.xml的dataSource标签中,其type属性就是配置连接池的种类.type的可取值 1.POO ...
- MyBatis基础入门《二十》动态SQL(foreach)
MyBatis基础入门<二十>动态SQL(foreach) 1. 迭代一个集合,通常用于in条件 2. 属性 > item > index > collection : ...
- MyBatis基础入门《十二》删除数据 - @Param参数
MyBatis基础入门<十二>删除数据 - @Param参数 描述: 删除数据,这里使用了@Param这个注解,其实在代码中,不使用这个注解也可以的.只是为了学习这个@Param注解,为此 ...
- MyBatis基础入门《二》Select查询
MyBatis基础入门<二>Select查询 使用MySQL数据库,创建表: SET NAMES utf8mb4; ; -- ---------------------------- -- ...
- mybatis基础系列(二)——基础语法、别名、输入映射、输出映射
增删改查 mapper根节点及其子节点 mybatis框架需要读取映射文件创建会话工厂,映射文件是以<mapper>作为根节点,在根节点中支持9个元素,分别为insert.update.d ...
- myBatis 基础测试 表关联关系配置 集合 测试
myBatis 基础测试 表关联关系配置 集合 测试 测试myelipse项目源码 sql 下载 http://download.csdn.net/detail/liangrui1988/599388 ...
- mybatis入门系列二之输入与输出参数
mybatis入门系列二之详解输入与输出参数 基础知识 mybatis规定mapp.xml中每一个SQL语句形式上只能有一个@parameterType和一个@resultType 1. 返回 ...
随机推荐
- 基于javaWeb阶段下的Cookie和Session总结
1. 会话技术 就是用户在使用浏览器浏览界面的时候,去访问多个页面后一次性关闭浏览器,这个过程叫会话,学习会话技术就是在客户端与服务器进行交互的时候为了能更好的保存数据.在java中会话技术只有C ...
- [DeeplearningAI笔记]序列模型1.7-1.9RNN对新序列采样/GRU门控循环神经网络
5.1循环序列模型 觉得有用的话,欢迎一起讨论相互学习~Follow Me 1.7对新序列采样 基于词汇进行采样模型 在训练完一个模型之后你想要知道模型学到了什么,一种非正式的方法就是进行一次新序列采 ...
- [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal
既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...
- vee-validate
http://vee-validate.logaretm.com/ 表单校验,配合VUE使用
- 图论:DFS序
DFS序可以把树转到区间上再用高级数据结构维护,比树链剖分好理解很多 一个闭区间就是一棵子树 POJ3321 #include<cstdio> ; ; int n,m,id,cnt; in ...
- 数学:FFT
在信息学竞赛中FFT只有一个用处那就是加速多项式的乘法 多项式乘法原本的时间复杂度是O(n^2)的,然后经过FFT之后可以优化为O(nlogn) FFT就是将系数表示法转化成点值表示法相乘,再由点值表 ...
- PHP扩展--taint检测隐藏漏洞
简介 Taint 可以用来检测隐藏的XSS code, SQL注入, Shell注入等漏洞, 并且这些漏洞如果要用静态分析工具去排查, 将会非常困难, 比如对于如下的例子: <?php echo ...
- MongoDB - MongoDB CRUD Operations, Update Documents
Update Methods MongoDB provides the following methods for updating documents in a collection: Method ...
- 使用asp.net改变图片颜色
最近奇葩经理提出了奇葩的需求,要能在网站上改变图片的颜色,比如灰色的变成彩色,彩色的变成灰色,尼玛楼主的感受你们不懂!于是有了下面的代码... 用法:调用update_pixelColor方法并传参数 ...
- node连接数据库(express+mysql)
操作是在ubuntu系统的下环境,简单记录一下过程. 首先用apt-get安装数据库,键入命令 sudo apt-get install mysql-server , 一路回车,然后在一个界面设置一下 ...