Mybatis02
别名配置
<typeAliases> <!-- a)内置别名 常用基本类型 和包装类型 8 个 以及集合类型 --> <!-- b)自定义别名 (1)单独配置类的实现 <typeAlias type="cn.itsource.mybatis.query.ProductQuery" alias="productQuery"/> <typeAlias type="cn.itsource.mybatis.domain.Product" alias="product"/>--> <!-- (2)配置包形式 推荐--> <package name="cn.itsource.mybatis.query"></package> <package name="cn.itsource.mybatis.domain"></package> </typeAliases>
内置别名
常用基本类型
自定义别名
sql映射器Mapper
1、根据需求,创建模型相关的Mapper接口(UserMapper)
2、编写映射文件
a)Mapper。Xml的命名空间,必须和接口的“全限定名”一致
b)定义sql标签的id,需要和“接口的方法”一致
3、配置映射文件
4、测试
<?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="cn.itsource._01_mapper.mapper.ProductMapper"> <select id="findAll" resultType="product"> select * from product </select> </mapper>
//测试类 public void testMapper(){ //得到mapper --映射器 (动态代理) ProductMapper productMapper = MybatisUtil.INSTANCE.getSqlSession(). getMapper(ProductMapper.class); for (Product product : productMapper.findAll()) { System.out.println(product); } }
高级查询
实现
1)定义映射去接口-EmployeeMapper List<Employee> query(EmployeeQuery query); 2)写映射文件并且导入核心配置文件 3)做测试
注意:
1 where:里面所有的条件如果都在前面加上and,并且最后会把第一个and替换为where
2 if 判断条件是否满足,如果是并且用and 3 模糊查询 方案1: 不能用# and ( name like %#{keywords}% or password like %#{keywords}% ) 方案2:用$ sql注入 and ( name like '%${keywords}%' or password like '%${keywords}%' ) 方案3:用mysql中字符串拼接函数concat and ( name like concat('%',#{keywords},'%') or password like '%${keywords}%' ) 4 如果有特殊符号
gt(>) ge(>=) lt(<) le(<=) 方案1:转义符号
<if test="maxSalePrice != null"> <!--and saleprice <= #{maxSalePrice} </if>
方案2:cdata
<![CDATA[ and saleprice <= #{maxSalePrice} ]]>
5 如果语句被多个地方调用可以使用sql include完成抽取和调用
(1)错误写法: <if test="productName != null"> and productName like '%#{productName}%' </if> 正确写法: --存在sql注入问题 <if test="productName != null"> and productName like '%${productName}%' </if> 正确写法: <if test="productName != null"> and productName like concat('%',#{productName},'%') </if>
结果映射(resultMap)
1.1. 为什么要使用结果映射
解决表字段名和对象属性名不一样的情况.
关联对象查询,在mybatis不会默认查询出来,需要自己查询结果并且通过resultMap来配置
1.2. 关联映射分类
一对一:一个员工只有一个身份证号。随意一方设计一个字段
多对一:多个员工对应一个部门。一般在多方设计一个一方属性 员工里面设计部门字段
一对多:一个部门拥有多个员工。还是在多方维护对象关联关系
多对多: 一个员工有多个角色。一个角色属于多个员工。用中间表来表示
本质上:多对一,一对一是一样的,都只是处理一个(association )。而一对多、多对多也是一样处理的都是集合(collection)
1.3. 关联映射处理方式
MyBatis提供两种方式处理我们关联对象,*嵌套查询*和*嵌套结果*。
嵌套结果: 发送1条SQL,查询所有的信息(本身+关联对象)
嵌套查询:发送1+N条sql。
处理数据库里面的列 和 对象里面的字段 不一统一的情况下
(1)使用别名就可以解决 <select id="findAll" resultType="product"> select id,productName pName,salePrice,costPrice,cutoff from product </select> (2)返回Map解决映射问题 <select id="findAll" resultMap="productMap"> select id,productName,salePrice,costPrice,cutoff from product </select> <resultMap id="productMap" type="product"> <!-- id:主键 property:类里面的属性 column:查询列名称 --> <id property="id" column="id"></id> <result property="pName" column="productName"></result> <result property="salePrice" column="salePrice"></result> </resultMap>
关系处理
多对一、一对一
嵌套结果
发一条关联sql解决问题,映射文件Mapper结果的手动封装ResultMap
嵌套查询
嵌套查询(发1查询user+N查询dept条sql解决问题,映射文件Mapper结果的自动封装ResultMap)
通过执行另外一个SQL映射语句来返回预期的复杂类型。
<!--嵌套结果--> <select id="findAll" resultMap="productMap"> SELECT p.id, p.productName, p.salePrice, p.costPrice, p.cutoff, dir.id did, dir.dirName dname FROM product p JOIN productDir dir ON p.dir_id = dir.id </select> <resultMap id="productMap" type="product"> <id property="id" column="id"></id> <result column="productName" property="pName"></result> <result column="salePrice" property="salePrice"></result> <result column="costPrice" property="costPrice"></result> <result column="cutoff" property="cutoff"></result> <!--处理一方--> <association property="dir" javaType="productDir"> <id column="did" property="id"></id> <result column="dname" property="dirName"></result> </association> <!--<result property="dir.id" column="did"></result> <result property="dir.dirName" column="dname"></result>--> </resultMap> <!--嵌套查询--> <!--<select id="findAll" resultMap="productMap"> select p.id,p.productName,p.salePrice,p.costPrice,p.cutoff,p.dir_id from product p </select> <resultMap id="productMap" type="product"> <id property="id" column="id"></id> <result column="productName" property="pName"></result> <result column="salePrice" property="salePrice"></result> <result column="costPrice" property="costPrice"></result> <result column="cutoff" property="cutoff"></result> <association property="dir" column="dir_id" javaType="productDir" select="selectDir"> </association> </resultMap> <select id="selectDir" parameterType="long" resultType="ProductDir"> select * from productDir where id = #{dir_id} </select>-->
一对多
<!--嵌套结果--> <!--<select id="findAll" resultMap="productDirMap"> select d.id,d.dirName,p.productName pName,p.salePrice,p.costPrice,p,cutoff from product p join productDir d on p.id = d.id limit 0,2 </select> <resultMap id="productDirMap" type="productDir"> <id property="id" column="id"></id> <result property="dirName" column="dirName"></result> <collection property="dirName" column="dirName"> <id property="id" column="id"></id> <result property="pName" column="productName"></result> <result property="salePrice" column="salePrice"></result> <result property="costPrice" column="costPrice"></result> </collection> </resultMap> --> <!--嵌套查询--> <select id="findAll" resultMap="productDirMap"> select dir.id,dir.dirName from productDir dir limit 0,2 </select> <resultMap id="productDirMap" type="productDir"> <id property="id" column="id"></id> <result property="dirName" column="id"></result> <collection property="products" column="id" ofType="product" select="selectProducts"> </collection> </resultMap> <select id="selectProducts" parameterType="long" resultType="product"> select id,productName pName,salePrice,costPrice,cutoff from product where dir_id = #{dir_id} </select>
多对多 和一对多一样
SSM集成
SpringMvc+Spring+Mybatis
框架集成核心
如果你的项目中,用到了Spring框架,那么其他框架主要就是和Spring集成!!
和Spring集成的核心思路
1、把当前框架的核心类,交给Spring管理
2、如果框架有事务,那么事务也要统一交给Spring管理
(1)创建项目--web项目 (maven/普通web)
(2)导入三个框架的jar包
(3)配置文件
applicationContext.xml --配置spring+mybatis
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <!--扫描service 自动注入到spring service注解 --> <context:component-scan base-package="cn.itsource.ssm.service"></context:component-scan> <!-- db.proproperties dataSource sqlSessionFactory transaction mapper--> <!--引入jdbc.properties--> <context:property-placeholder location="classpath:db.properties" /> <!--创建dataSource --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!--sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="mapperLocations" value="classpath:cn/itsource/ssm/mapper/*Mapper.xml"></property> <property name="typeAliasesPackage"> <value> cn.itsource.ssm.domain </value> </property> </bean> <!-- 把产生mapper 交给 spring--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.itsource.ssm.mapper"></property> </bean> <!--事务配置--> <!--配置事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!--开启事务注解的支持,默认会去找一个名称叫做transactionManager的事务管理器 --> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>
applicationCotnext-mvc.xml --配置springmvc
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--扫描controller--> <context:component-scan base-package="cn.itsource.ssm.web.controller" /> <!--静态资源处理--> <mvc:default-servlet-handler /> <!--识别@RequestMapping等注解支持--> <mvc:annotation-driven /> <!--配置视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
db.properties --配置库
web.xml --配置web的内容
log4j.properties
扩展
扩展一
<resultMap id="productMap" type="product"> <id column="id" property="id"></id> <result column="productName" property="pName"></result> <result column="salePrice" property="salePrice"></result> <result column="costPrice" property="costPrice"></result> <result column="cutoff" property="cutoff"></result> <!-- 处理一方--> <!--<association property="dir" javaType="productDir"> <id column="did" property="id"></id> <result column="dname" property="dirName"></result> </association>--> <!-- 扩展1--> <result property="dir.id" column="did"></result> <result property="dir.dirName" column="dname"></result>
</resultMap>
扩展二
分页 一对多使用 嵌套结果去分页 ,存在小问题
建议使用嵌套查询:
<select id="findAll" resultMap="productDirMap"> select dir.id ,dir.dirName from productDir dir limit 0,2 </select> <resultMap id="productDirMap" type="productDir"> <id property="id" column="id"></id> <result property="dirName" column="dirName"></result> <collection property="products" column="id" ofType="product" select="selectProducts"> </collection> </resultMap> <select id="selectProducts" parameterType="long" resultType="product"> select id,productName pName,salePrice,costPrice,cutoff from product where dir_id = #{dir_id} </select>