SSM学习09Spring与MyBatis的整合之传统DAO方式整合

通过spring和mybatis整合查询数据库:

  新建项目导入相应的jar:

spring中的jar包:

mybatis中的jar包:

mybatis和spring整合中间jar包下载地址:

https://mvnrepository.com/artifact/org.mybatis/mybatis-spring/1.3.1

mysql数据库驱动jar包,以及数据源所需jar包:

commons-dbcp2-2.1.1.jar

commons-pooI2-2.4.2.jar

这两个的下载地址:http://commons.apache.org/proper/commons-dbcp/download_dbcp.cgi     http://commons.apache.org/proper/commons-pool/download_pool.cgi

 所有jar包截图:

  

数据库中的t_customer表在前面的学习中已经建好了。

  在src目录下创建db.properties文件,Spring配置文件applicationContext.xml以及MyBatis的配置文件mybatis-config.xml,log4j.properties

  applicationContext.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 4     xmlns:aop="http://www.springframework.org/schema/aop"
 5     xmlns:tx="http://www.springframework.org/schema/tx" 
 6     xmlns:context="http://www.springframework.org/schema/context"
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 8     http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
 9     http://www.springframework.org/schema/tx 
10     http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
11     http://www.springframework.org/schema/context 
12     http://www.springframework.org/schema/context/spring-context-4.3.xsd
13     http://www.springframework.org/schema/aop 
14     http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
15     <context:property-placeholder location="classpath:db.properties"/>
16     <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
17     <property name="driverClassName" value="${jdbc.driver}"></property>
18     <property name="url" value="${jdbc.url}"></property>
19     <property name="username" value="${jdbc.username}"></property>
20     <property name="password" value="${jdbc.password}"></property>
21 <!--     最大连接数 -->
22     <property name="maxTotal" value="${jdbc.maxTotal}"></property>
23 <!--     最大空闲连接 -->
24     <property name="maxIdle" value="${jdbc.maxIdle}"></property>
25 <!--     初始化连接数 -->
26     <property name="initialSize" value="${jdbc.initialSize}"></property>
27     </bean>
28     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
29     <property name="dataSource" ref="dataSource"></property>
30     </bean>
31     <tx:annotation-driven transaction-manager="transactionManager"/>
32     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
33     <property name="dataSource" ref="dataSource"></property>
34     <property name="configLocation" value="classpath:mybatis-config.xml"></property>
35     </bean>
36     <bean id="customerDao" class="com.zyk.dao.impl.CustomerDaoImpl">
37     <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
38     </bean>
39     </beans>

  mybatis-config.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 3      "http://mybatis.org/dtd/mybatis-3-config.dtd">
 4      <configuration>
 5          <properties resource="db.properties"></properties>
 6          <typeAliases>
 7              <package name="com.zyk.po"/>
 8          </typeAliases>
 9          <mappers>
10          <mapper resource="com/zyk/po/CustomerMapper.xml"/>
11          </mappers>
12      </configuration>

db.prooerties:

1 jdbc.driver=com.mysql.jdbc.Driver
2 jdbc.url=jdbc:mysql://localhost:3306/mybatis
3 jdbc.username=root
4 jdbc.password=123456
5 jdbc.maxTotal=30
6 jdbc.maxIdle=10
7 jdbc.initialSize=5

log4j.properties:

1 # Global logging configuration
2 log4j.rootLogger=ERROR, stdout
3 # MyBatis logging configuration...
4 log4j.logger.com.zyk=DEBUG
5 # Console output...
6 log4j.appender.stdout=org.apache.log4j.ConsoleAppender
7 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
8 log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

新建src目录com.zyk.po包,创建Customer类:

 1 package com.zyk.po;
 2  
 3 public class Customer {
 4     private int id;
 5     private String username;
 6     private String jobs;
 7     private String phone;
 8     public int getId() {
 9         return id;
10     }
11     public void setId(int id) {
12         this.id = id;
13     }
14     public String getUsername() {
15         return username;
16     }
17     public void setUsername(String username) {
18         this.username = username;
19     }
20     public String getJobs() {
21         return jobs;
22     }
23     public void setJobs(String jobs) {
24         this.jobs = jobs;
25     }
26     public String getPhone() {
27         return phone;
28     }
29     public void setPhone(String phone) {
30         this.phone = phone;
31     }
32     @Override
33     public String toString() {
34         return "Customer [id="+id+",username="+username+",jobs="+jobs+",phone="+phone+"]";
35     }
36     
37  
38 }

新建com.zyk.dao,创建CustomerDao类:

1 package com.zyk.dao;
2 
3 import com.zyk.po.Customer;
4 
5 public interface CustomerDao {
6     public Customer findCustomerById(int id);
7 }

新建com.zyk.dao.impl创建CustomerDaoImpl类实现CustomerDao:

package com.zyk.dao.impl;

import org.mybatis.spring.support.SqlSessionDaoSupport;

import com.zyk.dao.CustomerDao;
import com.zyk.po.Customer;

public class CustomerDaoImpl extends SqlSessionDaoSupport implements CustomerDao {

    @Override
    public Customer findCustomerById(int id) {
        // TODO Auto-generated method stub
        
        return this.getSqlSession().selectOne("com.zyk.po"+".CustomerMapper.findCustomerById", id);
    }

}

在com.zyk.po中写CustomerMapper.xml:

1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
3     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
4    <mapper namespace="com.zyk.po.CustomerMapper">
5    <select id="findCustomerById" parameterType="int" resultType="com.zyk.po.Customer">
6    select * from t_customer where id=#{id}
7    </select>
8    
9    </mapper>

写测试类DaoTest:

  

 1 package com.zyk.test;
 2 
 3 
 4 import org.springframework.context.ApplicationContext;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 import org.junit.Test;
 7 
 8 
 9 import com.zyk.dao.CustomerDao;
10 import com.zyk.po.Customer;
11 
12 public class DaoTest {
13     @Test
14     public void findCustomerByIdDaoTest() {
15             ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
16             CustomerDao customerDao=applicationContext.getBean(CustomerDao.class);
17             Customer customer=customerDao.findCustomerById(1);
18             System.out.println(customer);
19     }
20 }

  实验结果:

 实验总结:

  本次实验是mybatis和spring的一起使用,数据库的连接从mybatis-config.xml中转移到applicationContext.xml,select语句还在CustomerMapper.xml中进行配置,然后在mybatis-config.xml添加配置。

用Dao层写查询方法,在Impl中实现使用mybatis,测试方法中和前面学习spring一样,进行测试。

猜你喜欢

转载自www.cnblogs.com/2312947032zyk/p/10573985.html