思路
需要spring通过单例方式管理sqlSessionFactory
spring和mybatis整合生成代理对象,使用sqlSessionFactory创建SqlSession。(spring和mybtis整合自动完成)
持久层的mapper都需要由spring进行管理
整合环境
- 导入所需的jar包
-
新建config包
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
log4j.properties
# Global logging configuration
#在开发环境下日志级别要设置成DEBUG,生成环境设置成info或ERROR
log4j.rootLogger=DEBUG, stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# 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
SqlMapConfig.xml
<?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="sqlmap/UserMapper.xml" />
</mappers>-->
<!--如果在spring中批量扫描mapper则在这里就不需要进行映射文件的配置-->
<!--加载映射文件-->
<!--<mappers>
<!–批量加载mapper–>
<package name="cn.persit.ssm.mapper" />
</mappers>-->
</configuration>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc "
default-autowire="byName">
<!--加载配置文件-->
<context:property-placeholder location="classpath:db.properties" />
<!--数据源,使用dbcp-->
<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>
<!--配置sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--加载mybatis的配置文件-->
<property name="configLocation" value="mybatis/SqlMapConfig.xml" />
<!--数据源-->
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
image.png
sqlSessionFactory
在applicationContext.xml配置sqlSessionFactory和数据源
sqlSessionFactory在mybatis和spring的整和包下
具体配置如上所示
mapper的开发(与spring整合后)
-
创建mapper.xml和mapper.java
image.png
<?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进行分类化管理,理解sql隔离
注意:使用mapper代理方法开发,namespace有特殊重要的作用,namespace等于mapper接口地址
-->
<mapper namespace="cn.persit.ssm.mapper.UserMapper">
<select id="findUserByID"
parameterType="cn.persit.ssm.entity.UserBean"
resultMap="cn.persit.ssm.entity.UserBean">
select * from user where id=#{id};
</select>
</mapper>
在applicationContext.xml中加载Mapper
<!--mapper配置
根据mapper接口生成mapper接口
-->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!--mapperInterface指定mapper接口-->
<property name="mapperInterface" value="cn.persit.ssm.mapper.UserMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
-
通过MapperScannerConfigurer进行mapper扫描
image.png
逆向工程
- 什么是逆向工程
mybatis需要程序员自己编写sql语句,mybatis官方提供逆向工程可以针对单表自动生成mybatis执行所需要的代码(mapper.java,mapper.xml,po类。。。。)
- 常用逆向工程方式:
由数据库的表生成Java代码 - mybatis在官方下载工程(maven方式)
<build>
<finalName>mybatisGenarator</finalName>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
网友评论