Mybatis学习第四天——Mybatis与Spring整合

主要介绍mapper配置与mapper的扫描配置,使用dao层的配置这里不多说。

1.导包

  1.1 Mybatis的jar包
  1.2 Spring的jar包
  1.3 Spring与Mybatis整合包
  1.4 数据库驱动包
  1.5 连接池的jar包

 

 

 2.配置Mybatis核心配置(SqlMapConfig.xml)

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE configuration
 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 <configuration>
 6     <!-- 设置别名 -->
 7     <typeAliases>
 8         <!-- 指定扫描包,会把包内所有的类都设置别名,别名的名称就是类名,大小写不敏感 -->
 9         <package name="com.mybatis.pojo" />
10     </typeAliases>
11     <!-- 加载mapper文件 -->
12     <mappers>
13         <!-- 采用加载包的形式 -->
14         <package name="com.mybatis.mapper"/>
15     </mappers>
16 </configuration>

该文件主要用于注册加载mapper文件。

3.log4j.propertis和db.properties文件

1 # Global logging configuration
2 log4j.rootLogger=DEBUG, stdout
3 # Console output...
4 log4j.appender.stdout=org.apache.log4j.ConsoleAppender
5 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
6 log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=asdf123456789

4.applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:context="http://www.springframework.org/schema/context" 
 4     xmlns:p="http://www.springframework.org/schema/p"
 5     xmlns:aop="http://www.springframework.org/schema/aop" 
 6     xmlns:tx="http://www.springframework.org/schema/tx"
 7     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 8     xsi:schemaLocation="
 9     http://www.springframework.org/schema/beans 
10     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
11     http://www.springframework.org/schema/context 
12     http://www.springframework.org/schema/context/spring-context-4.0.xsd
13     http://www.springframework.org/schema/aop 
14     http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
15     http://www.springframework.org/schema/tx 
16     http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
17     http://www.springframework.org/schema/util 
18     http://www.springframework.org/schema/util/spring-util-4.0.xsd">
19 
20    <!-- 加载配置文件 -->
21    <context:property-placeholder location="classpath:db.properties" />
22 
23     <!-- 数据库连接池 -->
24     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
25         destroy-method="close">
26         <property name="driverClassName" value="${jdbc.driver}" />
27         <property name="url" value="${jdbc.url}" />
28         <property name="username" value="${jdbc.username}" />
29         <property name="password" value="${jdbc.password}" />
30         <property name="maxActive" value="10" />
31         <property name="maxIdle" value="5" />
32     </bean>
33 
34     <!-- 配置SqlSessionFactory -->
35     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
36         <!-- 配置mybatis核心配置文件 -->
37         <property name="configLocation" value="classpath:SqlMapConfig.xml" />
38         <!-- 配置数据源 -->
39         <property name="dataSource" ref="dataSource" />
40     </bean>
41     <!-- 
42     <bean id="UserDao" class="com.mybatis.dao.impl.UserDaoImpl">
43         <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
44     </bean>
45      -->
46     <!--  
47         mapper方式:
48             配置对应的mapper工厂,设置sqlSession工厂与mapper接口
49     
50     <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
51         <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
52         <property name="mapperInterface" value="com.mybatis.mapper.UserMapper"></property>
53     </bean>
54     -->
55     <!-- 配置mapper扫描设置 -->
56     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
57     <!-- 只需要设置基本包路径 -->
58         <property name="basePackage" value="com.mybatis.mapper"></property>
59     </bean>
60 
61 </beans>

 

  

 

 

 

posted @ 2019-01-16 22:53  洞若观火  阅读(424)  评论(0)    收藏  举报