企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] # 简介 1. sqlSessionFactory对象应该放到spring容器中作为单例存在 2. 传统dao的开发中,应该从spring容器中获得sqlsession对象 3. Mapper代理形式中,应该从spring容器中直接获得mapper的代理对象 4. 数据库的连接以及数据库连接池事务管理都交给spring容器来完成 # 加入配置文件 数据库连接及连接池 sqlsessionFactory对象,配置到spring容器中 mapper代理对象或者是dao实现类配置到spring容器中 创建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> <!-- 设置别名 --> <typeAliases> <!-- 指定扫描包,会把包内所有的类都设置别名,别名的名称就是类名,大小写不敏感 --> <package name="pojo" /> </typeAliases> <mappers> <package name="mapper" /> </mappers> </configuration> ~~~ 创建applicationContext.xml ~~~ <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <context:property-placeholder location="classpath:db.properties"/> <!-- 数据库连接池 --> <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> <!-- Mybatis的工厂 --> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 核心配置文件的位置 --> <property name="configLocation" value="classpath:sqlMapConfig.xml"/> </bean> <!-- Mapper动态代理开发 扫描 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 基本包 --> <property name="basePackage" value="mapper" /> </bean> </beans> ~~~ 创建mapper包 在mapper包下创建 UserMapper.xml ~~~ <?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"> <mapper namespace="mapper.UserMapper"> <!-- 通过ID查询一个用户 --> <select id="findUserById" parameterType="Integer" resultType="User"> select * from user where id = #{v} </select> </mapper> ~~~ 创建UserMapper.java ~~~ package mapper; import pojo.User; public interface UserMapper { public User findUserById(Integer id); } ~~~ 然后我们测试下 ~~~ ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); UserMapper mapper = ac.getBean(UserMapper.class); User user = mapper.findUserById(10); System.out.println(user); ~~~