码迷,mamicode.com
首页 > 编程语言 > 详细

Mybatis-spring 传统dao开发

时间:2019-04-08 01:23:10      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:under   cte   EAP   sql文件   rda   cti   jdb   连接   doc   

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开发

标签:under   cte   EAP   sql文件   rda   cti   jdb   连接   doc   

原文地址:https://www.cnblogs.com/jinyu-helloword/p/10668190.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!