Mybatis-spring 传统dao开发

Posted jinyu-helloword

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis-spring 传统dao开发相关的知识,希望对你有一定的参考价值。

jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456

log4j.properties

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

1、mybatis核心配置文件sqlMapperConfig.xml 加载mapper文件

<?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>
   <mappers>
       <mapper resource="mybatis/user.xml"/>
   </mappers>
</configuration>

2、spring核心配置文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

   <!-- 加载配置文件 -->
   <context:property-placeholder location="classpath:jdbc.properties" />

    <!-- 数据库连接池 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <!-- 连接池的最大数据库连接数 -->
        <property name="maxActive" value="10" />
        <!-- 最大空闲数 -->
        <property name="maxIdle" value="5" />
    </bean>
    <!--  SqlSessionFactoryBean 配置-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 注入数据源 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 加载 mybatis核心文件sqlMapperConfig配置文件-->
        <property name="configLocation" value="classpath:sqlMapperConfig.xml" />
        <!-- 别名包扫描-->
        <property name="typeAliasesPackage" value="com.cn.pojo"/>
    </bean>
    <!-- 传统dao开发 -->
    <bean class="com.cn.dao.impl.UserDaoImpl">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
    
</beans>

3、UserDao.java

package com.cn.dao;


import com.cn.pojo.User;

public interface UserDao {
    User getUserById(Integer id);
}

4.UserDaoImpl.java  extends SqlSessionDaoSupport implements UserDao

package com.cn.dao.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import com.cn.dao.UserDao;
import com.cn.pojo.User;

public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao{

    @Override
    public User getUserById(Integer id) {
        SqlSession sqlSession = super.getSqlSession();
        User selectOne = sqlSession.selectOne("user.getUserById",id);
//        不能关闭sqlSession
        return selectOne;
    }
}

4.mapper文件user.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">
<!-- namespace命名空间,隔离sql文件 -->
<!-- #{}占位符相当于jdbc的? -->
<!-- ${value} 字符串拼接 -->
<mapper namespace="user">
    <select id="getUserById" parameterType="int" resultType="user">
        select id, username,birthday,sex,address from user where id = #{id}
    </select>
    
</mapper>

 

5、将UserDaoImpl注入到bean

 <!-- 传统dao开发 -->
    <bean class="com.cn.dao.impl.UserDaoImpl">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>

5.测试UserDaoTest.java

加载spring核心文件

applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
package com.cn.dao;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.cn.pojo.User;

public class UserDaoTest {
    private ApplicationContext applicationContext;
    
    @Before
    public void init() {
        applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    }
    @Test
    public void test() {
        System.out.println(1);
        UserDao userdao = applicationContext.getBean(UserDao.class);
        User userById = userdao.getUserById(1);
        System.out.println(userById);
    }

}

 

以上是关于Mybatis-spring 传统dao开发的主要内容,如果未能解决你的问题,请参考以下文章

MyBatis-Spring的sqlSessionTemplate

MyBatis与Spring整合

传统开发与微服务开发理解

MyBatis-03-笔记

Mybatis——Dao层实现映射文件深入核心配置文件深入

mybatis开发dao的方式