spring和mybatis的使用心的

1.spring和mybatis的整合
 a)数据源的配置(通过properties引入数据)
 b)spring和mybatis的扫包(spring需要扫包,mybatis也需要扫包)
 c)全局懒加载的配置
 d)事务管理者的配置
 e)注解驱动的配置
 f)dao层映射配置(保证映射名称一致,切勿忘记大小写问题)

<?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:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
	   http://www.springframework.org/schema/tx
	   http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <!--开启事务注解驱动-->
    <tx:annotation-driven />

    <!--加载配置文件-->
    <context:property-placeholder location="classpath:db.properties"/>
    <!--配置数据源-->
    <bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="username" value="${user}"/>
        <property name="url" value="${url}"/>
        <property name="password" value="${password}"/>
        <property name="driverClassName" value="${driver}"/>
    </bean>

    <!--加载mybatis的核心配置文件-->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="datasource"/>
        <property name="configLocation" value="classpath:SqlMapConfig.xml"/>
    </bean>

    <!--配置事务管理者-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="datasource"></property>
    </bean>

    <!--扫描dao-->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.brant.dao"/>
    </bean>

    <!--扫描-->
    <context:component-scan base-package="com.brant"></context:component-scan>

</beans>

2.使用mybatis查询返回值为空
 a)当时mybatis查询数据时,存在返回值为空的情况
 b)因此,可以在controller和dao层中做以下处理,将返回值封装成一个Map集合再进行返回

@Override
    public Map findUserById(int user_id) {
        Map userMap = new HashMap();
        UserInfo user = userDao.findUserById(user_id);

        if(user != null){
            userMap.put("user",user);
            return userMap;
        }
        return userMap;
    }

3.一层嵌套延迟加载和两层嵌套延迟加载https://www.cnblogs.com/kenhome/p/7764398.html
 a)开启全局延迟加载

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--
根标签是configuration
-->
<configuration>
    <!--全局懒加载    -->
    <settings>
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="aggressiveLazyLoading" value="false"/>
    </settings>
    <!--批量定义别名-->
    <typeAliases>
        <package name="com.wangshidai.eshopFront.pojo"/>
        <package name="com.wangshidai.eshopFront.entity"/>
    </typeAliases>
</configuration>

b)一层嵌套延迟加载

<mapper namespace="com.brant.dao.BookDao">

    <resultMap id="findBookOneTypeMap" type="RootTypeInfo" autoMapping="true">
        <id column="type_id" property="type_id" />
        <collection property="oneTypeList" select="com.brant.dao.BookDao.findBookOneType" autoMapping="true" column="type_id">
        </collection>

    </resultMap>
    <select id="findBookType" resultMap="findBookOneTypeMap">
        select * from tb_type where parent_id = 0
    </select>

    <select id="findBookOneType" parameterType="String" resultType="RootTypeInfo">
        select * from tb_type where parent_id = #{type_id}
    </select>
</mapper>

. c)两层嵌套延迟加载(省份,城市,地区三级联动)

<mapper namespace="com.wangshidai.eshopFront.dao.AddressDao">
    <!--省份连接城市-->
    <resultMap id="cityListMap" type="Province" autoMapping="true">
        <id column="provinceid" property="provinceid" />
        <collection property="citys" autoMapping="true" select="com.wangshidai.eshopFront.dao.AddressDao.findCity" column="provinceid" />
    </resultMap>
    <select id="findProvince" ofType="Province" resultMap="cityListMap">
        select * from provinces
    </select>
    <!--城市连接地区-->
    <resultMap id="areaListMap" type="City" autoMapping="true">
        <id column="cityid" property="cityid" />
        <collection property="areas" autoMapping="true" select="com.wangshidai.eshopFront.dao.AddressDao.findArea" column="cityid"/>
    </resultMap>
    <select id="findCity" parameterType="String" ofType="City" resultMap="areaListMap">
        select * from cities where provinceid = #{provinceid}
    </select>

    <select id="findArea" parameterType="String" resultType="Area">
        select * from areas where cityid = #{cityid}
    </select>
</mapper>

4.jackson解析数据时,出现异常

Could not write JSON: No serializer found for class org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory$EnhancedResultObjectProxyImpl and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS);

.  1)解决:在jackson解析的Bean类加上@JsonIgnoreProperties(value = “handler”) 注解,并实现序列化
5.mybatis获取插入值自增长ID

<insert id="insert" parameterType="Spares"     
        useGeneratedKeys="true" keyProperty="id">    
        insert into system(name) values(#{name})    
</insert>
<!-- 
@Override
    public int register(UserInfo userInfo) {
        int count = regDao.register(userInfo);
        return userInfo.getUser_id() ;
    }
//获取到的ID就是插入的ID,通过形参中userDao对象获取
-->

. a)注意:keyProperty和java对象的属性名称保持一致,传递的参数必须是Bean

上一篇:【TcaplusDB知识库】[Generic表]批量读取数据示例代码


下一篇:Sqlserver Json