Mybatis的开发方式(Mybatis与数据库交互方式)

xiaoxiao2021-03-01  23

Mybatis工具类

public class MybatisUtil { static SqlSessionFactory sqlSessionFactory; static { String resource = "mybatis-config.xml"; InputStream inputStream; try { inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 该方法调用必须在应用程序的成员方法中调用 * @return */ public static SqlSession findSqlSession(){ return sqlSessionFactory.openSession(); } }

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="dao.UserMapper"> <select id="selectAll" resultType="model.User"> select * from USER </select> <!-- OGNL表达式 1.简单类型参数,#{}中参数的命名可以自定义 2.POJO参数,#{}中参数的命名必须是POJO对像中get方法的后缀(首字母小写)一致。 --> <select id="selectById" resultType="model.User"> select * from USER where id = #{id} </select> <select id="selectByItem" resultType="model.User" parameterType="model.User"> select * from USER where username=#{username} and password=#{password} </select> <insert id="insert" parameterType="model.User"> insert into USER(username,password) values(#{username},#{password}) </insert> <delete id="delete" parameterType="model.User"> delete from USER where id = #{id} </delete> <update id="update" parameterType="model.User"> update USER set username=#{username},password=#{password} where id = #{id} </update> </mapper>

dao层代码

public class UserDao { public void insert(User user) { SqlSession session = MybatisUtil.findSqlSession(); try { session.insert("dao.UserMapper.insert", user); // 提交事务 session.commit(); } catch (Exception e) { e.printStackTrace(); session.rollback(); } finally { session.close(); } } public void delete(User user) { SqlSession session = MybatisUtil.findSqlSession(); try { session.delete("dao.UserMapper.delete", user); // 提交事务 session.commit(); } catch (Exception e) { e.printStackTrace(); session.rollback(); } finally { session.close(); } } public void update(User user) { SqlSession session = MybatisUtil.findSqlSession(); try { session.update("dao.UserMapper.update", user); // 提交事务 session.commit(); } catch (Exception e) { e.printStackTrace(); session.rollback(); } finally { session.close(); } } public User selectById(int id) { SqlSession session = MybatisUtil.findSqlSession(); User user = null; try { user = session.selectOne("dao.UserMapper.selectById", id); } catch (Exception e) { e.printStackTrace(); } finally { session.close(); } return user; } public List<User> selectAll() { SqlSession session = MybatisUtil.findSqlSession(); List<User> users = null; try { users = session.selectList("dao.UserMapper.selectAll"); } catch (Exception e) { e.printStackTrace(); } finally { session.close(); } return users; } public User selectByItem(User user) { SqlSession session = MybatisUtil.findSqlSession(); try { user = session.selectOne("dao.UserMapper.selectByItem", user); } catch (Exception e) { e.printStackTrace(); } finally { session.close(); } return user; } }

service层代码

public class UserService { UserDao dao = new UserDao(); public void save(User user) { dao.insert(user); } public void remove(User user) { dao.delete(user); } public void modify(User user) { dao.update(user); } public User findById(int id) { return dao.selectById(id); } public List<User> findAll() { return dao.selectAll(); } public User findByItem(User user) { return dao.selectByItem(user); } }

单元测试类 省略...

转载请注明原文地址: https://www.6miu.com/read-4149934.html

最新回复(0)