mabits
来源:互联网 发布:淘宝一口价怎么改 编辑:程序博客网 时间:2024/06/06 13:08
一、传入参数的传递
parameterType指定参数类型
基本类型参数(int、string.......)pojo类型:user对象map类型包装类型
1、map类型的传递
需求:查询用户性别为男,姓张的用户
<mapper namespace="com.itcast.dao.UserMapper" >
<!-- 需求:查询用户性别为男,姓张的用户 -->
<select id="findUserWithMap" parameterType="map" resultType="user" >
select * from user where sex=#{sex} and username like "%"#{username}"%"
</select>
</mapper>
@Test
public void fun1() throws IOException{
InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
Map<String, Object> map = new HashMap<>();
map.put("sex", "男");
map.put("username", "张");
List<User> list = userMapper.findUserWithMap(map);
System.out.println(list);
}
@Test
public void fun1() throws IOException{
InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
Map<String, Object> map = new HashMap<>();
map.put("sex", "男");
map.put("username", "张");
List<User> list = userMapper.findUserWithMap(map);
System.out.println(list);
}
2、包装类型的传递
需求:查询用户性别为男,姓张的用户
<!-- 需求:查询用户性别为男,姓张的用户 -->
<select id="findUserWithQueryVo" parameterType="queryvo" resultType="user" >
select * from user where sex=#{user.sex} and username like "%"#{user.username}"%"
</select>
@Test
public void fun2() throws IOException{
InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = new User();
user.setSex("男");
user.setUsername("张");
QueryVo queryVo = new QueryVo();
queryVo.setUser(user);
List<User> list = userMapper.findUserWithQueryVo(queryVo);
System.out.println(list);
}
二、返回参数的类型
基本类型pojo类型返回集合类型
1、返回基本类型
需求:查询用户性别为男,姓张的用户总计数
<!-- 查询用户性别为男,姓张的用户总记录数 -->
<select id="findUserWithQueryVoCount" parameterType="queryvo" resultType="int" >
select count(2) from user where sex=#{user.sex} and username like "%"#{user.username}"%"
</select>
@Test
public void fun3() throws IOException{
InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = new User();
user.setSex("男");
user.setUsername("张");
QueryVo queryVo = new QueryVo();
queryVo.setUser(user);
int count = userMapper.findUserWithQueryVoCount(queryVo);
System.out.println(count);
}
2、resultType特性
特性:查询数据库列名必须和映射的javabean属性名称一一对应,且名称相同,否则不能映射成功
验证:
<!-- 验证resultType的映射特性-->
<select id="findUserWithResultType" resultType="user" >
select id _id,username _username,birthday _birthday,sex _sex,address _address from user
</select>
@Test
public void fun4() throws IOException{
InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> list= userMapper.findUserWithResultType();
System.out.println(list);
}
返回结果错误:[null, null, null, null, null, null, null, null, null]
解决映射不成功的方案:
* 定义别名
* resultMap
3、resultMap映射特点
解决resultType 数据库列名与javabean属性名不相同不能映射的问题
需要先定义一个resultMap的映射关系
主键映射 :使用id
column:查询数据库列名
property:javabean属性名
<!-- 定义resultMap -->
<resultMap type="user" id="userMap">
<!-- 主键映射 -->
<id column="_id" property="id" />
<!-- 普通属性映射 -->
<result column="_username" property="username" />
<result column="_birthday" property="birthday" />
<result column="_sex" property="sex" />
<result column="_address" property="address" />
</resultMap>
<!-- resultMap解决数据库列名与javabean属性名不相同不能映射的状况-->
<select id="findUserWithResultMap" resultMap="userMap" >
select id _id,username _username,birthday _birthday,sex _sex,address _address from user
</select>
java代码和上面一样
三、多表关联的查询:
1、多的一方 一对一查询
可以用resultType也可以用resultMap查询
① 用resultType来映射
需求:通过订单查询用户和订单所有数据,一个订单只对应一个用户,所以是一对一查询新定义一个订单实体类,再用一个OrdersCustom类继承Orders类中所有属性,再添加User类中的所有属性,注意把User类中的主键名字改成uid,因为不能和Orders的主键id名字重复啊,查询后的结果就封装在OrdersCustom这个类的属性中
<mapper namespace="com.itcast.dao.OrdersMapper" >
- <select id="findOrdersWithUserByResultType" resultType="ordersCustom" >
select o.id,o.user_id userId,o.number,o.createtime createTime,o.note,
u.id uid,u.username,u.birthday,u.sex,u.address
from user u,orders o where u.id=o.user_id
</select>
</mapper>
@Test
public void fun1() throws IOException{
InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
OrdersMapper ordersMapper = sqlSession.getMapper(OrdersMapper.class);
List<OrdersCustom> list = ordersMapper.findOrdersWithUserByResultType();
System.out.println(list);
}
②用resultMap来映射
配置一对一关系映射:association
property:指定映射orders中那个属性
javaType:指定关系映射对象类型
<mapper namespace="com.itcast.dao.OrdersMapper" >
<!--
定义关系映射:resultMap
type:指定映射类型
id:resultMap映射关系唯一标识
-->
<resultMap type="orders" id="ordersMap">
<!--
主键映射 :使用id
column:查询数据库列名
property:javabean属性名
-->
<id column="id" property="id" />
<!-- 普通属性映射 -->
<result column="user_id" property="userId" />
<result column="number" property="number" />
<result column="createtime" property="createTime" />
<result column="note" property="note" />
<!-- 表关联
配置一对一关系映射:association
property:指定映射orders中那个属性
javaType:指定关系映射对象类型
-->
<association property="user" javaType="user">
<id column="uid" property="id" />
<result column="username" property="username" />
<result column="birthday" property="birthday" />
<result column="sex" property="sex" />
<result column="address" property="address" />
</association>
</resultMap>
<select id="findOrdersWithUserByResultMap" resultMap="ordersMap" >
SELECT orders.*,user.id uid,user.username,user.birthday,user.sex,user.address
FROM orders,user WHERE orders.user_id = user.id
</select>
</mapper>
@Test
public void fun2() throws IOException{
InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
OrdersMapper ordersMapper = sqlSession.getMapper(OrdersMapper.class);
List<Orders> list = ordersMapper.findOrdersWithUserByResultMap();
System.out.println(list);
}
2、一的一方 一对多查询
一个用户对应了多个订单,如果通过用户查询用户数据和对应的订单数据,那就是一对多查询
一对多查询只能用resultMap来映射
<mapper namespace="com.itcast.dao.UserMapper" >
<resultMap type="user" id="userMap">
<id column="uid" property="id" />
<result column="username" property="username"/>
<result column="birthday" property="birthday"/>
<result column="sex" property="sex"/>
<result column="address" property="address"/>
<!-- ofType:制定映射的javabean类型,property:表示映射到user中的那个属性 -->
<collection property="oList" ofType="orders" >
<!--
主键映射 :使用id
column:查询数据库列名
property:javabean属性名
-->
<id column="id" property="id"/>
<!-- 普通属性映射 -->
<result column="user_id" property="userId"/>
<result column="number" property="number"/>
<result column="createtime" property="createTime"/>
<result column="note" property="note"/>
</collection>
</resultMap>
<!-- 查询用户订单,一对多 -->
<select id="findUserWithOrdersByMap" resultMap="userMap" >
select user.id uid,user.username,user.birthday,user.sex,user.address,
orders.id,orders.user_id userId,orders.number,orders.createtime createTime,orders.note
from user,orders where orders.user_id=user.id
</select>
</mapper>
@Test
public void fun3() throws IOException{
InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> list = userMapper.findUserWithOrdersByMap();
System.out.println(list);
}
四、动态sql查询
1、if标签和where标签连用
where标签自动生成where语句,并且where标签可以自动去掉sql语句的第一个多余的“and”
if标签::当if标签传入的是pojo类型,或者包装类型,test里面就直接写传入类型里面的属性名,但是当传入的parameterType是基本数据类型,例如string,如要判断string类型的内容,则需要在test里面判断:_parameter
①不用where标签的做法:
<!-- 需求:查询用户性别为男,姓张的用户 -->
<select id="findUserWithQueryVo" parameterType="queryvo" resultType="user" >
select * from user where 1=1
<if test="user.sex!=null and user.sex!=''">
andsex=#{user.sex}
</if>
<if test="user.username!=null and user.username!=''">
and username like "%"#{user.username}"%"
</if>
</select>
@Test
public void fun2() throws IOException{
InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = new User();
user.setSex(null);
user.setUsername("张");
QueryVo queryVo = new QueryVo();
queryVo.setUser(user);
List<User> list = userMapper.findUserWithQueryVo(queryVo);
System.out.println(list);
}
②用where标签的做法:
<!-- 需求:查询用户性别为男,姓张的用户 -->
<select id="findUserWithQueryVo" parameterType="queryvo" resultType="user" >
select * from user
<!-- where标签自动生成where语句,并且去掉第一个多余的and -->
<where>
<if test="user.sex!=null and user.sex!=''">
andsex=#{user.sex}
</if>
<if test="user.username!=null and user.username!=''">
and username like "%"#{user.username}"%"
</if>
</where>
</select>
java程序和上面一样
2、动态更新语句:
<set>标签类似于where标签
3、sql片段
把相同的sql片段抽取出去
<sql id="" ></sql>标签定义片段;;<include refid="" ></include>来引入sql片段id和refid相对应
<mapper namespace="com.itcast.dao.UserMapper" >
<!-- 定义一个sql片段,抽取公共的sql语句 -->
<sql id="where_if_vo">
<where>
<if test="user.sex!=null and user.sex!=''">
andsex=#{user.sex}
</if>
<if test="user.username!=null and user.username!=''">
and username like "%"#{user.username}"%"
</if>
</where>
</sql>
<!-- 需求:查询用户性别为男,姓张的用户 -->
<select id="findUserWithQueryVo" parameterType="queryvo" resultType="user" >
select * from user
<!-- where标签自动生成where语句,并且去掉第一个多余的and -->
<include refid="where_if_vo"></include>
</select>
<!-- 查询用户性别为男,姓张的用户总记录数 -->
<select id="findUserWithQueryVoCount" parameterType="queryvo" resultType="int" >
select count(2) from user
<include refid="where_if_vo"></include>
</select>
</mapper>
4、foreach动态遍历集合参数
①第一种方法:
select * from user where (id=22 or id=23 or id=33)
<!-- 接收集合参数 -->
<select id="findUserWithOr" parameterType="queryvo" resultType="user" >
select * from user
<where>
<foreach collection="ids" item="id" open="(" separator="or" close=")" >
id=#{id}
</foreach>
</where>
</select>
@Test
public void fun4() throws IOException{
InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
QueryVo queryVo = new QueryVo();
List<Integer> ids = new ArrayList<>();
ids.add(22);
ids.add(24);
ids.add(26);
queryVo.setIds(ids);
List<User> list = userMapper.findUserWithOr(queryVo);
System.out.println(list);
}
②另一种方法:
select * from user where id in (22,23,33)
<!-- 接收集合参数 -->
<select id="findUserWithIn" parameterType="queryvo" resultType="user" >
select * from user
<where>
<foreach collection="ids" item="id" open="id IN(" separator="," close=")" >
#{id}
</foreach>
</where>
</select>
java代码只要改个方法名即可
arraylist、list=======》list
数组====》array
五、mybatis和spring整合
spring配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 整合持久层框架 -->
<!-- 数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" >
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 创建工厂,生成sqlSession -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
<property name="dataSource" ref="dataSource" ></property>
<!-- 扫描别名 -->
<property name="typeAliasesPackage" value="com.itcast.domain" ></property>
<!-- 加载sqlMapConfig.xml文件 -->
<property name="configLocation" value="classpath:sqlMapConfig.xml" ></property>
</bean>
<!-- 扫描接口,开启接口代理方式开发 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
<property name="basePackage" value="com.itcast.dao" ></property>
</bean>
</beans>
另一种扫描接口的方法:
<bean class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="cn.itcast.mybatis.mapper.UserMapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
或者直接在sqlMapConfig.xml里面配置
1、传统模式整合:
applicationContext.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 整合持久层框架 -->
<!-- 数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" >
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 创建工厂,生成sqlSession -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
<property name="dataSource" ref="dataSource" ></property>
<!-- 扫描别名 -->
<property name="typeAliasesPackage" value="com.itcast.domain" ></property>
<!-- 加载sqlMapConfig.xml文件 -->
<property name="configLocation" value="classpath:sqlMapConfig.xml" ></property>
</bean>
<!-- 定义dao对象,注入工厂对象 -->
<bean class="com.itcast.dao.impl.UserMapperImpl" >
<property name="sqlSessionFactory" ref="sqlSessionFactory" ></property>
</bean>
</beans>
sqlMapConfig.xml配置:
<configuration>
<mappers>
<!-- <package name="com.itcast.dao"/> -->
<mapper resource="com/itcast/dao/UserMapper.xml" />
</mappers>
</configuration>
使用传统模式,有实现类
public class UserMapperImpl extends SqlSessionDaoSupport implements UserMapper{
@Override
public List<User> findUserWithQueryVo(QueryVo queryVo) {
List<User> list = this.getSqlSession().selectList("com.itcast.dao.UserMapper.findUserWithQueryVo", queryVo);
return list;
}
}
测试类:
@Test
public void fun2() throws IOException{
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
UserMapperImpl mapperImpl = context.getBean(UserMapperImpl.class);
User user = new User();
user.setSex("男");
user.setUsername("张");
QueryVo queryVo = new QueryVo();
queryVo.setUser(user);
List<User> list = mapperImpl.findUserWithQueryVo(queryVo);
System.out.println(list);
}
2、接口代理模式整合:
applicationContext.xml加上接口扫描即可
测试:
UserMapper bean = context.getBean(UserMapper.class);
阅读全文
1 0
- mabits
- 学习spring mabits
- mabits批量insert、update、select
- mysql 关联查询 修改 ssm mabits
- mabits逆向工程生成通用mapper
- ssm mabits 遇到Mapped Statements collection does not contain value。
- 非web项目,maven工程整合spring+mabits,并打包为可运行jar包
- Mabits的PageHelper分页插件的使用和Jsp分页页面展示(带源码)
- 题目1163:素数 2008年北京航空航天大学计算机研究生机试真题
- 初识UML(二)
- mui页面间传值
- Linux下RPM包管理
- LINQ系列:Linq to Object集合操作符
- mabits
- 高仿淘宝商品详情标题栏渐变
- strus2中Action方法将 base64 字符串转成 图片文件实体
- github与oschina的README.md显示图片
- HDU 3709
- 计算机网络基础详解
- NOIP2015 [stone][substring]
- ftp文件上传,可以上传至非根目录的指定目录,并解决文件中文无法上传问题,返回文件路径
- Git安装之Window Mac