During the previous project, the DaO layer wrote some spring JDBC, which is not very convenient to use. Today I learned the new framework: mybatis. replace the previously written content with the content set up by the mybatis framework.
First you want to go to the mybatis official website to go to The mybatis jar package: mybatis-3.2.7.jar. Because I am on the basis of spring to build mybatis so also need to get a mybatis-spring-1.2.2.jar, this connection package seems to be in spring official is not found, need to go online to find.
Go to the topic. First create a mybitis-config.xml under SRC:
<?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="com/bookstore/mappers/config/UserMapper.xml"/> </mappers> </configuration>
This indicates that the Mapper path can define multiple mapper.
Create a usermapper. xml file under the Path COM/bookstore/mappers/config.
<?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="UserMapper"> <select id="selectUser" parameterType="String" resultType="User"> select userName,password,email,phone,isVip from user where userName=#{userName} </select> <insert id="addUser" parameterType="User"> insert into user (userName,password,email,phone,isVip) values (#{userName},#{password},#{email},#{phone},#{isVip}) </insert> </mapper>
I simply wrote a query and an insert.
To put it simply, the core of mybatis is a sqlsession. mybatis creates it in the factory mode.
During spring integration, sqlseesionfactory is injected by bean, and sqlsession is produced and then obtained by sqlsessiondaosupport.
After obtaining sqlsession, you can use various SQL methods.
Now I have written a basedao In the DaO layer. It inherits sqlsessiondaosupport and does not do anything. Create a userdaomappperdaoimpl to inherit basedao, so that userdaomapperdaoimpl can use sqlsessiondaosupport to get sessionsql.
Userdaomapperdaoimpl:
package com.bookstore.dao.impl;import java.util.List;import com.bookstore.dao.UserDao;import com.bookstore.dao.impl.BaseDao;import com.bookstore.model.User;public class UserDaoMapperImpl extends BaseDao implements UserDao { public void addUser(User user) { this.getSqlSession().insert("UserMapper.addUser", user); } public User getUser(String userName) { return this.getSqlSession().selectOne("UserMapper.selectUser" ,userName); }}
Finally, you must configure it in Spring:
Let's take a look at it first. First, sqlsessionfactory wants to get datesource, and datesource should include the connection pool. Basedao should have a property called sqlsessionfactory. As mentioned earlier, basedao inherits an abstract class sqlsessiondaosupport, and then a userdaomapperimpl class. You only need to inherit one basedao to have the attributes sqlsessionfactory and sqlsessiondaosupport.
Spring-config.xml:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="typeAliasesPackage" value="com.bookstore.model"></property> </bean> <bean id="baseDao" class="com.bookstore.dao.impl.BaseDao"> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> <bean id="userDao" class="com.bookstore.dao.impl.UserDaoMapperImpl" parent="baseDao"> </bean>
I found that basedao did not write parent, but userdao did. Later I added parent to basedao, but an error occurred. It turns out that the two inherited classes are abstract classes. Therefore, you do not need to write.
Similarly, if I remove this basedao class, I will directly let userdaomapperimpl inherit sqlsessionsupport and then add attributes.
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
The same is true.