mybatis02
映射器Mapper
<?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>
测试类 测试Mapper
@Test
public void testMapper(){
//得到mapper --映射器 (动态代理)
ProductMapper productMapper = MybatisUtil.INSTANCE.getSqlSession().
getMapper(ProductMapper.class);
for (Product product : productMapper.findAll()) {
System.out.println(product);
}
}
高级查询
(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>
(2)特殊符号转义:
<!-- salePrice minPrice 和maxPrice之间 特殊符号需要转义-->
<!-- <if test="minPrice != null and maxPrice != null">
and salePrice > #{minPrice} and salePrice < #{maxPrice}
</if>-->
<if test="minPrice != null and maxPrice != null">
<![CDATA[
and salePrice > #{minPrice} and salePrice <= #{maxPrice}
]]>
</if>
结果映射
处理数据库里面的列 和 对象里面的字段 不一统一的情况下
(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
嵌套查询: 发送 1+n条sql
<?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._03_manytoone.mapper.ProductMapper">
<!-- <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>
</resultMap>-->
<!-- (1)嵌套结果 发送一条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>-->
<!-- 嵌套查询:发送多sql语句(1+n条sql)-->
<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 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>
<!--根据上面查询dir_id 在去查询 分类对象-->
<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>
</mapper>
对多方处理
嵌套结果和前台查询
<?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._04_onetomany.mapper.ProductDirMapper">
<!-- 嵌套结果-->
<!-- <select id="findAll" resultMap="productDirMap">
select dir.id ,dir.dirName,
p.id pid,p.productName pName,p.salePrice,p.costPrice,p.cutoff
from productDir dir join product p
on dir.id = p.dir_id
</select>
<resultMap id="productDirMap" type="productDir">
<id property="id" column="id"></id>
<result property="dirName" column="dirName"></result>
<collection property="products" ofType="product">
<id property="id" column="pid"></id>
<result property="pName" column="pName"></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
</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>
</mapper>
ssm项目整合
步骤
(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
扩展
1
<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>
2
分页 一对多使用 嵌套结果去分页 ,存在小问题
<select id="findAll" resultMap="productDirMap">
select dir.id ,dir.dirName,
p.id pid,p.productName pName,p.salePrice,p.costPrice,p.cutoff
from productDir dir join product p
on dir.id = p.dir_id
limit 0,2
</select>
<resultMap id="productDirMap" type="productDir">
<id property="id" column="id"></id>
<result property="dirName" column="dirName"></result>
<collection property="products" ofType="product">
<id property="id" column="pid"></id>
<result property="pName" column="pName"></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="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>
常见错误
写的mapper文件没有在配置文件里面注册
java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for cn.itsource.mybatis.crud.UserMapper.createTable
配置找不到方法
返回的结果是List,而需要的是一个对象
org.apache.ibatis.executor.ExecutorException: Statement returned more than one row, where no more than one was expected.
没有序列化异常
使用缓存,如果报序列化问题,让类去实现序列化接口
implements Serializable
一、什么是序列化--面试题
把java对象转换二进制过程
二、什么情况下使用序列化
1、在网络中直接传输一个java对象。
2、在数据库的Blob(二进制)字段(列)中,直接存一个java对象会出问题
3、当前,需要把java对象放入内存(本地的临时文件)中
java.io.ObjectInputStream
java.io.ObjectOutputStream
4.HttpSession里面存放对象,tomcat的内存不足(500M)的时候,···钝化到硬盘