整合数据库连接池dbcp
1.导入jar包
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
2.修改beans.xml头文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
mlns: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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启注解-->
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.xszx"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
3.配置数据源beans.xml
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
整合mybatis
1.删除所有dao层的实现层
2.导入包(使用maven进行管理)
<!--mybaits核心包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
<!--日志:记录应用程序所有的执行过程-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--整合mybatis与spring需要的jar包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!--整合spring 内置数据源-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
3.配置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>
<!--配置日志输出-->
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<!--配置mybatis的映射文件-->
<mappers>
<mapper resource="dao/UserMapper.xml"></mapper>
<!-- 注解配置下方语句-->
<!-- <mapper class="com.xszx.dao.UserMapper"></mapper>-->
</mappers>
</configuration>
4. beans.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<!-- 开启注解驱动-->
<context:annotation-config/>
<!-- 开启组件扫描-->
<context:component-scan base-package="com.xszx"></context:component-scan>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!--创建sqlSessionFactory对象-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据源对象-->
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<!--创建sqlSessionTemplate对象-->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
</bean>
</beans>
5.业务层(注入SqlSessionTemplate)
@Service
public class UserServiceImpl implements UserService {
private SqlSessionTemplate sqlSessionTemplate;
@Autowired
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
@Override
public void adduser(User user) {
UserDao userDao = sqlSessionTemplate.getMapper(UserDao.class);
userDao.adduser(user);
}
}
6.测试
public class UserTest {
public static void main(String[] args){
BeanFactory beanFactory = new ClassPathXmlApplicationContext("beans.xml");
UserController userController = (UserController) beanFactory.getBean("userController");
userController.adduser();
}
}
至此整合技术结束,若有任何疑问欢迎大家在评论区讨论或者后台私信我!!!
求赞求赞,关注一下我吧,各位大官人!!!给大家磕一个!!!