一、开发框架结构
1、引入jar包
2、配置文件
3、po
4、dao
二、编写dao接口和接口实现类
public interface UserDao { public User findUserById(int id);
dao实现类
public class UserDaoImpl implements UserDao { @Override public User findUserById(int id) { //1、获取SqlSession SqlSession sqlSession= null; //2、通过SqlSession的方法执行statement User user=sqlSession.selectOne("",id); //3、返回执行结果 return user; }
1、获取statement的id
1.1、新建一个Mapper.xml(User.xml)用于配置操作数据库的statement
<?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="test"> <select id="findUserById" parameterType="Integer" resultType="com.xxx.mybatis.spring.po.User"> SELECT * FROM T_USER WHERE ID=#{ID} </select> </mapper>
1.2、配置核心配置文件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="User.xml"/> </mappers> </configuration>
2、获取SqlSession对象
2.1、让UserDaoImpl实现类继承SqlSessionDaoSupport,SqlSessionDaoSupport可以获得SqlSession对象
public abstract class SqlSessionDaoSupport extends DaoSupport { private SqlSessionTemplate sqlSessionTemplate; public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) { if (this.sqlSessionTemplate == null || sqlSessionFactory != this.sqlSessionTemplate.getSqlSessionFactory()) { this.sqlSessionTemplate = createSqlSessionTemplate(sqlSessionFactory); } }
2.2、SqlSession对象需要sqlSessionFactory对象创建的,sqlSessionFactory对象要通过spring创建,spring创建sqlSessionFactory,就需要数据源和mybatis核心配置文件
<!-- 1、引入数据库连接属性文件 --> <context:property-placeholder location="classpath:jdbc.properties" /> <!-- 2、配置C3P0连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- 3、spring创建sqlSessionFactory对象 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 加载mybatis的配置文件 --> <property name="configLocation" value="mybatis/SqlMapConfig.xml" /> <!-- 数据源 --> <property name="dataSource" ref="dataSource" /> </bean> <!-- 4、spring创建userDao对象,并将sqlSessionFactory对象注入到userDao对象的sqlSessionFactory属性中--> <bean id="userDao" class="com.xxx.mybatis.spring.dao.impl.UserDaoImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean>
以上就解决了SqlSession和statement的获取,dao的实现类的代码如下:
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao { @Override public User findUserById(int id) { //1、获取SqlSession SqlSession sqlSession= this.getSqlSession(); //2、通过SqlSession的方法执行statement User user=sqlSession.selectOne("test.findUserById",id); //3、返回执行结果 return user; }
三、测试
ApplicationContext applicationContext; @BeforeEach void setUp() throws Exception { applicationContext=new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml"); } @Test void testFindUserById() { UserDao userDao=(UserDao) applicationContext.getBean("userDao");//userDao是spring管理Bean的id User user=userDao.findUserById(1); System.out.println(user); }