ssm整合时,mybatis的动态sql处理

一、前提

        在看这篇文章是,要对ssm整合有一定的基础,本篇并不是讲如何整合ssm的,而是将mybatis里面的静态sql转换为动态sql

二、静态sql的情况

        一般的调用查询的流程是jsp-->controller-->service->dao,而我要将的是dao层的情况

以findAll(查询所有)findByPage(分页查询)为例子,上代码:

@Repository
public class AccountDaoImpl implements AccountDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public List<Account> findByPage(Integer kw, Integer cp) {
        try {
            List<Account> list =jdbcTemplate.query("select * from account limit ?,?",new AccountRowMapper(), kw, cp);
            return list;
        } catch (DataAccessException e) {
            throw new RuntimeException(e);
        }
    }
    @Override
    public List<Account> findAll() {
        try {
            List<Account> accounts = jdbcTemplate.query("select * from account",new AccountRowMapper());
            return accounts;
        } catch (DataAccessException e) {
            throw new RuntimeException(e);
        }
    }
}

可以看出,上面我用的是JDBCTemplate技术来操作sql,但是直接写在dao中的,并不是我们需要的结果

三、改为动态sql

       1、 将JDBCTemplate换成SqlSession来处理sql语句,因为JDBCTemplate是spring框架自带的连接池,并不能动态处理sql

       2、 在配置文件中将SqlSessionFactory工厂以及SqlSession加载到spring的bean中

       3、 将数据库资源文件(dataSource)和dao的映射文件设置到工厂中

spring与mybatis整合的配置文件(这里我将JDBCTempate注释掉了,看起来比较明显):

<?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"
       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">
    <context:component-scan base-package="com.it">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <!--    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">-->
    <!--        <property name="dataSource" ref="dataSource"/>-->
    <!--    </bean>-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/ssm?serverTimezone=Asia/Shanghai"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    </bean>
    <bean id="sessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg ref="sqlSessionFactory"/>
    </bean>
</beans>

        这里的工厂里需要配置数据库文件以及对于的dao映射文件(我的文件放在了resources下的mapper包中)

具体代码(accountMapper.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">
<mapper namespace="com.it.dao.AccountDao">
    <select id="findAll" resultMap="accountMap">
        select *
        from account
    </select>
    <select id="findByPage" resultMap="accountMap" parameterType="java.util.Map">
        select * from account limit #{kw},#{cp}
    </select>
    <resultMap id="accountMap" type="com.it.pojo.Account">
    <id column="id" property="id"/>
        <result column="name" property="username"/>
        <result column="money" property="money"/>
    </resultMap>
</mapper>

注意

        在映射文件中如果要用到resultMap,必须要放在sql语句的下面,否则会出现找不到resultMap的情况

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值