整合后,无需提交session.commit();
无需关闭session.close();
Log4j.properties // 日志记录
- log4j.rootLogger=DEBUG,Console
- log4j.appender.Console=org.apache.log4j.ConsoleAppender
- log4j.appender.Console.layout=org.apache.log4j.PatternLayout
- log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
- log4j.logger.org.apache=DEBUG
|
目录结构
一、mapper代理开发
- <!-- mapper开发 -->
- <bean
id="userDao"
class="org.mybatis.spring.mapper.MapperFactoryBean">
- <property
name="mapperInterface"
value="cn.tri.sm.dao.UserDao"></property>
- <property
name="sqlSessionFactory"
ref="sqlSessionFactory"></property>
- </bean>
|
二、Dao继承SqlSessionDaoSupport
1、整合jar包
- <dependencies>
- <dependency><!--隐含beans\core\aop\expression-->
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>4.3.20.RELEASE</version>
- </dependency>
-
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- <version>4.3.20.RELEASE</version>
- </dependency>
-
- <!-- 事物spring-tx -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-tx</artifactId>
- <version>4.3.20.RELEASE</version>
- </dependency>
-
- <!-- spring 整合 mybaties
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis-spring</artifactId>
- <version>1.3.2</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-jdbc</artifactId>
- <version>4.3.20.RELEASE</version>
- </dependency>
-
- <!-- mysql jdbc -->
- <dependency>
- <groupId>org.testcontainers</groupId>
- <artifactId>mysql</artifactId>
- <version>1.11.1</version>
- <scope>test</scope>
- </dependency>
-
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.6</version>
- </dependency>
-
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis</artifactId>
- <version>3.4.6</version>
- </dependency>
-
- <!-- commons包 -->
- <dependency>
- <groupId>commons-pool</groupId>
- <artifactId>commons-pool</artifactId>
- <version>1.6</version>
- </dependency>
-
- <!-- 数据源 -->
- <dependency>
- <groupId>commons-dbcp</groupId>
- <artifactId>commons-dbcp</artifactId>
- <version>1.4</version>
- </dependency>
- <!--日志-->
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>1.2.17</version>
- </dependency>
- </dependencies>
|
2、配置文件
applicationContext.xml
- <?xml
version="1.0"
encoding="UTF-8"?>
- <beans
xmlns="http://www.springframework.org/schema/beans"
- 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">
- <!-- 数据源 -->
- <bean
id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
- <property
name="driverClassName"
value="com.mysql.jdbc.Driver"
/>
- <property
name="url"
value="jdbc:mysql://localhost:3306/project"
/>
- <property
name="username"
value="root"
/>
- <property
name="password"
value="admin"
/>
- </bean>
-
- <!-- 拿到sqlSessionFactory>>mybatis-spring.jar提供 -->
- <bean
id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
- <property
name="dataSource"
ref="dataSource"/>
- <!-- 读取mybatis配置文件-->
- <!-- <property name="configLocation" value="classpath:mybaties-config.xml"></property> -->
- <!-- 扫描mapper配置文件-->
- <property
name="mapperLocations"
value="classpath:cn\tri\sm\mapper\UserDao.xml"></property>
- <!-- 取别名-->
- <property
name="typeAliasesPackage"
value="cn.tri.sm.pojo"></property>
- </bean>
-
- <bean
id="userDao"
class="cn.tri.sm.daoImpl.UserDaoImpl">
- <property
name="sqlSessionFactory"
ref="sqlSessionFactory"></property>
- </bean>
-
- <bean
id="userService"
class="cn.tri.sm.serviceImpl.UserServiceImpl">
- <property
name="userDao"
ref="userDao"></property>
- </bean>
- </beans>
|
Mybatis-config.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>
- <!-- 取别名 -->
- <typeAliases>
- <package
name="cn.tri.sm.pojo"/>
- <!-- <package name="cn.tri.sm.dao"/> -->
- <package
name="cn.tri.sm.daoImpl"/>
- </typeAliases>
- <!-- 扫描包路径 -->
- <mappers>
- ????????<mapper
resource="cn\tri\sm\mapper\UserDao.xml"/>
- </mappers>
- </configuration>
|
3、demo
daoImpl
- public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao{
-
- public
void
addUser(User user) {
- SqlSession session =
super.getSqlSession();
- session.insert("UserDao.addUser", user);
- System.out.println("添加成功");
- }
- }
|
serviceImpl
- public class UserServiceImpl implements UserService{
- private UserDao userDao;
-
- public
void
setUserDao(UserDao userDao) {
- this.userDao = userDao;
- }
-
- public
void
addUser(User user) {
- userDao.addUser(user);
- }
- }
|
Servlet
- public class TestAddUser {
- public static
void
main(String[] args) {
- ApplicationContext context =
new
ClassPathXmlApplicationContext("applicationContext.xml");
- UserService us = (UserService) context.getBean("userService");
- User u =
new
User(2,
"yefan",
"888888");
- us.addUser(u);
- }
- }
|
三、
<!-- mapper开发,第三方式。 交给MapperScannerConfigurer 类,批量扫描mapper接口,
注意name="sqlSessionFactoryBeanName" ,且此时为value="sqlSessionFactory",而不是ref
扫描到的mapper接口,springIOC管理后,id="首字母小写接口名"
- <bean
id="mappers"
class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property
name="sqlSessionFactoryBeanName"
value="sqlSessionFactory"></property>
- <property
name="basePackage"
value="cn.tri.sm.dao"></property>
- </bean>
-
- <bean
id="userService"
class="cn.tri.sm.serviceImpl.UserServiceImpl">
- <property
name="userDao"
ref="userDao"></property>
- </bean>
|
标签:tri,userDao,name,Spring,整合,spring,mybatis,org
来源: https://www.cnblogs.com/tiankx/p/13837875.html