【框架】mybatis 原始dao开发方法

需要写dao接口和dao实现类

需要向dao实现类注入SqlSessionFactory,在方法体内通过SqlSessionFactory创建SqlSession

1、dao接口

public interface UserDao {
	//查找
	public User finUserById(int id);
	//删
	public void delelteUserById(int id);
	//增
	public void insertUser(User user);
	//改
	public void updateUserById(User user);
}

2.dao接口的实现类

public class UserDaoImple implements UserDao {
    
	private SqlSessionFactory sqlSessionFactory;
	//通过构造函数注入SqlSessionFactory
	public UserDaoImple(SqlSessionFactory sqlSessionFactory){
		this.sqlSessionFactory = sqlSessionFactory;
	}
	
	@Override
	public User finUserById(int id) {
		SqlSession sqlSession =null;
		User user;
		try {
			sqlSession = sqlSessionFactory.openSession();
			
			user = sqlSession.selectOne("test.findUserById", id);
			System.out.println(user);
			sqlSession.commit();
			
			return user;
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
		sqlSession.close();	
		}
		
		return null;
	}

	@Override
	public void delelteUserById(int id) {
		SqlSession sqlSession =null;
		try {
			sqlSession =  sqlSessionFactory.openSession();
			sqlSession.delete("test.deleteUserById", id);
			sqlSession.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			sqlSession.close();	
		}
	}

	@Override
	public void insertUser(User user) {
		SqlSession sqlSession =null;
		
		try {
			sqlSession =  sqlSessionFactory.openSession();
			
			sqlSession.insert("test.insertUser", user);
			sqlSession.commit();
		} catch (Exception e) {
			
			e.printStackTrace();
		}finally{
			sqlSession.close();	
		}

	}

	@Override
	public void updateUserById(User user) {
		SqlSession sqlSession =null;
		
		try {
			sqlSession = sqlSessionFactory.openSession();
			
			sqlSession.update("test.insertUser", user);
			user.setAddress("laojia");
			sqlSession.commit();
		} catch (Exception e) {
			
			e.printStackTrace();
		}finally{
			sqlSession.close();	
		}
	}

}

 SqlSession一定要定义在方法内,因为SlqSession是线程不安全的,在SqlSesion实现类中除了有接口中的方法(操作数据库的方法)还有数据域属性(成员变量)

3、配置mapper.xml文件

<mapper namespace="test">
	<select id="findUserById" parameterType="int" resultType="com.mybatis.pojo.User">
		SELECT * FROM USER WHERE id=#{id}
	</select>
	
	<select id="findUserByName" parameterType="java.lang.String" resultType="com.mybatis.pojo.User">
		SELECT * FROM USER WHERE username like '%${value}%'
	</select>
	
	<insert id="insertUser" parameterType="com.mybatis.pojo.User">
		
		<!-- <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
			SELECT LAST_INSERT_ID()
		</selectKey> -->
		
		<selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String">
			SELECT UUID()
		</selectKey>
	 	insert into user(id,username,birthday,sex,address) value (#{id},#{username},#{birthday},#{sex},#{address}) 
		<!-- insert into user(username,birthday,sex,address) value (#{username},#{birthday},#{sex},#{address})  -->
	</insert>
	
	<delete id="deleteUserById" parameterType="java.lang.String">
		delete from user where id=#{id}
	</delete>
	
	<update id="upadteUserById" parameterType="com.mybatis.pojo.User" >
		update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id}
	</update>

 4、junit测试

 

	@Test
	public void testFindUserById() throws IOException{
		String resource = "SqlMapConfig.xml";
		
		InputStream inputStream = Resources.getResourceAsStream(resource);
		
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		
		UserDao userdao = new UserDaoImple(sqlSessionFactory);
		
		User user = userdao.finUserById(27);
		
		System.out.println(user);
		
	}

原始 dao开发存在的问题:

1、dao接口实现类方法中存在大量模板方法,设想能否将这些代码提取出来,大大减轻程序员的工作量。

2、调用sqlsession方法时将statement的id硬编码了

3、调用sqlsession方法时传入的变量,由于sqlsession方法使用泛型,即使变量类型传入错误,在编译阶段也不报错,不利于程序员开发。

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值