一.SqlSession使用范围
1.SqlSessionFactoryBuilder
通过SqlSessionFactoryBuilder
创建会话工厂SqlSessionFactory
将SqlSessionFactoryBuilder
当作一个工具类使用即可,不需要使用单例管理SqlSessionFactoryBuilder
。
在需要创建SqlSessionFactory
时候,只需要new一次SqlSessionFactoryBuilder
即可
2.SqlSessionFactory
通过SqlSessionFactory创建SqlSession使用单例模式管理SqlSessionFactory(工厂一旦创建,使用一个实例)
将来mybatis和spring整合后使用单例模式管理sqlSessionFactory
3.SqlSession
SqlSession是一个面向用户(程序员)的接口
SqlSession中提供了很多操作数据库的方法,例如:selectOne/selectList
sqlSession是线程不安全的,在SqlSession实现类中除了有接口中的方法(操作数据库的方法)还有ukulel域属性
SqlSession最佳应用场合在方法体内,定义成局部变量使用
二.原始dao开发方法(程序员只需要写dao接口和dao实现类)
开发环境及配置文件:mybatis(增删改查)入门程序 ——学习笔记
接口:AdminDao.java
package dao;
import cn.huan.mybatis.AdminBean;
/**
* Created by 马欢欢 on 2017/6/29.
*/
public interface AdminDao {
/**
* 根据id查询用户信息
* @param id
* @return
* @throws Exception
*/
public AdminBean findUserById(int id) throws Exception;
/**
* 添加用户信息
* @param adminBean
* @throws Exception
*/
public void insertUser(AdminBean adminBean) throws Exception;
/**
* 删除用户信息
* @param id
* @throws Exception
*/
public void deleteYser(int id) throws Exception;
}
实现:AdminDaoImpl.java
package dao;
import cn.huan.mybatis.AdminBean;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
/**
* Created by 马欢欢 on 2017/6/29.
*/
public class AdminDaoImpl implements AdminDao{
//需要向dao实现类中注入SqlSessionFactory
//这里通过构造方法注入
private SqlSessionFactory sqlSessionFactory;
public AdminDaoImpl(SqlSessionFactory sqlSessionFactory){
this.sqlSessionFactory = sqlSessionFactory;
}
public AdminBean findUserById(int id) throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
AdminBean adminBean =sqlSession.selectOne("test.findUserById",id);
sqlSession.close();
return adminBean;
}
public void insertUser(AdminBean adminBean) throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
sqlSession.insert("test.insertUser",adminBean);
sqlSession.commit();
sqlSession.close();
}
public void deleteYser(int id) throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
sqlSession.insert("test.deleteUser",id);
sqlSession.commit();
sqlSession.close();
}
}
映射文件:adminMap.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="test">
<!--通过id查找用户-->
<select id="findUserById" parameterType="int" resultType="cn.huan.mybatis.AdminBean">
SELECT * FROM admin WHERE a_id =#{a_id}
</select>
<!--模糊查询-->
<select id="findUserByName" parameterType="java.lang.String" resultType="cn.huan.mybatis.AdminBean">
SELECT * FROM admin WHERE a_username LIKE '%${value}%'
</select>
<!--添加用户-->
<insert id="insertUser" parameterType="cn.huan.mybatis.AdminBean">
<!--将插入数据的主键返回
SELECT Last_insert_ID 得到刚插入的主键值 只适用与自增主键
keyProperty :将查询到主键值设置到parameterType指定的对象的那个属性
order:SELECT Last_insert_ID执行顺序,相对于insert语句来说它的执行顺序-->
<selectKey keyProperty="a_id" order="AFTER" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>
INSERT INTO admin (a_nameid,a_username,a_password,a_trank)
VALUES(#{a_nameid},#{a_username},#{a_password},#{a_trank})
</insert>
<!--删除用户 通过id-->
<delete id="deleteUser" parameterType="java.lang.Integer">
DELETE from admin where a_id =#{a_id}
</delete>
<!--更新用户信息通过id-->
<update id="updateUser" parameterType="cn.huan.mybatis.AdminBean">
UPDATE admin set a_nameid=#{a_nameid},a_username=#{a_username},a_password=#{a_password},a_trank=#{a_trank} where a_id=#{a_id}
</update>
</mapper>
测试:AdminTest.java
package dao;
import cn.huan.mybatis.AdminBean;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
/**
* Created by 马欢欢 on 2017/6/29.
*/
public class AdminTest {
private SqlSessionFactory sqlSessionFactory;
@Before
//测试前执行
public void setUp() throws IOException {
//配置文件
String resource = "mybatis-config.xml";
//的到配置文件流
InputStream inputStream = Resources.getResourceAsStream(resource);
//创建会话工厂
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void testFinUserById() throws Exception {
AdminDao adminDao= new AdminDaoImpl(sqlSessionFactory);
AdminBean adminBean1 =adminDao.findUserById(1);
System.out.println(adminBean1);
}
}
总结问题:
1.dao接口实现类方法总中存在大量的模板方法
2.在调用sqlSession方法时时将statement的id硬编码了
3.调用sqlsession方法时传入的变量,由于sqlsession方法使用泛型,即使变量传错也不提示,不利于开发。
三.mapper代理方法(我们只需要mapper接口(相当于dao接口))
1.在mapper.xml中,namespace等于mapper接口地址
2.mapper.java接口中的方法名和mapper.xml中statement的id一致
3.mapper.java接口中的方法输入参数类型和mapper.xml中statement的parameterType指定类型一致。
4.mapper.java接口中的方法返回值类型和mapper.xml中statement的resultType一致
1.接口:AdminMapper.java
package cn.mapper;
import cn.huan.mybatis.AdminBean;
/**
* Created by 马欢欢 on 2017/6/29.
*/
public interface AdminMapper {
/**
* 根据id查询用户信息
* @param id
* @return
* @throws Exception
*/
public AdminBean findUserById(int id) throws Exception;
2.映射文件:mapper.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="cn.mapper.AdminMapper">
<!--通过id查找用户-->
<select id="findUserById" parameterType="int" resultType="cn.huan.mybatis.AdminBean">
SELECT * FROM admin WHERE a_id =#{a_id}
</select>
</mapper>
配置文件: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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/sams?serverTimezone=UTC "></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</dataSource>
</environment>
</environments>
<!--加载映射文件-->
<mappers>
<mapper resource="mapper/adminMap.xml"></mapper>
<mapper resource="mapper/mapper.xml"></mapper>
</mappers>
</configuration>
测试类:AdminMapperTest.java
package cn.mapper;
import cn.huan.mybatis.AdminBean;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
/**
* Created by 马欢欢 on 2017/6/29.
*/
public class AdminMapperTest {
private SqlSessionFactory sqlSessionFactory;
@Before
//测试前执行
public void setUp() throws IOException {
//配置文件
String resource = "mybatis-config.xml";
//的到配置文件流
InputStream inputStream = Resources.getResourceAsStream(resource);
//创建会话工厂
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void testFinUserById() throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
//创建AdminMapper对象,mybatis自动生成mapper代理对象
AdminMapper adminMapper = sqlSession.getMapper(AdminMapper.class);
//调用adminMapper的方法
AdminBean adminBean = adminMapper.findUserById(2);
System.out.println(adminBean);
}
}
上一篇:mybatis(增删改查)入门程序 ——学习笔记