目标: 利用Mybatis实现增删改查
Mybatis官方文档: http://www.mybatis.org/mybatis-3/zh/index.html
<mappers>
<mapper resource="UserMapper.xml">mapper>
mappers>
这里定义所有实现增删改查的方法。
<mapper namespace="">
mapper>
private SqlSessionFactory sessionFactory;
@Before
public void init() throws IOException {
//创建 SqlSessionFactoryBuilder
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
//解析核心配置文件
InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
sessionFactory = sqlSessionFactoryBuilder.build(is);
}
SqlSession sqlSession = sessionFactory.openSession();
//创建dap接口的实现类
UserDao dao = sqlSession.getMapper(UserDao.class);
<--查询表中的所有数据-->
<select id="findAll" resultType="com.domain.User">
SELECT * FROM USER ;
select>
<select id="findUserById" parameterType="int" resultType="com.domain.User">
SELECT * from user WHERE id = #{id} ;
select>
<select id="findUserByName" parameterType="com.domain.User"
resultType="com.domain.User">
SELECT * FROM USER WHERE username LIKE '%${username}%' ;
select>
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
//解析核心配置文件
InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory sessionFactory = sqlSessionFactoryBuilder.build(is);
SqlSession sqlSession = sessionFactory.openSession();
//创建dap接口的实现
UserDao dao = sqlSession.getMapper(UserDao.class);
//调用方法
List<User> list=dao.findAll();
for (User user : list) {
System.out.println(user);
}
//调用方法
User userById = dao.findUserById(13);
//提交事务
sqlSession.commit();
sqlSession.close();
System.out.println(userById);
//调用方法
User user = new User();
user.setUsername("王");
List userByName = dao.findUserByName(user);
//提交事务
sqlSession.commit();
sqlSession.close();
System.out.println(userByName);
<insert id="addUser" parameterType="com.domain.User" keyProperty="id" useGeneratedKeys="true">
insert into user (username,sex,birthday,address)
values (#{username},#{sex},#{birthday},#{address}) ;
insert>
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
//解析核心配置文件
InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
sessionFactory = sqlSessionFactoryBuilder.build(is);
SqlSession sqlSession = sessionFactory.openSession();
//创建dap接口的实现
UserDao dao = sqlSession.getMapper(UserDao.class);
因为上面的语句每次都会使用,所以将他们提取出来,到 1.4 中,下面的语句不再写。
//调用方法
User user= new User();
user.setUsername("Saber");
user.setSex("女");
user.setBirthday(new Date());
user.setAddress("英灵之战");
//将信息封装到user实体中
dao.addUser(user);
//提交事务
sqlSession.commit();
sqlSession.close();
System.out.println("最新添加的id:"+user.getId());
}
<update id="updateUser" parameterType="com.domain.User">
UPDATE user set sex = #{sex} WHERE id = #{id};
update>
//调用方法
User user = new User();
user.setSex("女");
user.setId(11);
int count = dao.updateUser(user);
//提交事务
sqlSession.commit();
sqlSession.close();
System.out.println("影响了:"+count);
<delete id="deleteUser" parameterType="com.domain.User">
DELETE FROM USER WHERE id= #{id};
delete>
ps:可以将com.domain.User改成int
//调用方法
User userById = dao.findUserById(13);
//提交事务
sqlSession.commit();
sqlSession.close();
System.out.println(userById);
与上面的一样。
在dao层,新建UserDaoImpl.java 文件,实现User接口中的方法。
private SqlSessionFactory sqlSessionFactory;
public UserDaoImpl(SqlSessionFactory factory){
this.sqlSessionFactory=factory;
}
//参数1:请求标识 namespace
//参数2:请求时的参数信息 (接口UserDao中的方法参数)
public List<User> findAll() {
//完成查询的所有信息,并将查询结果返回
SqlSession sqlSession = sqlSessionFactory.openSession();
//调用框架提供的方法完成查询所有用户信息
//参数1:请求标识 namespace
//参数2:请求时的参数信息
List<User> list = sqlSession.selectList("UserDaoImpl.findAll");
sqlSession.commit();
sqlSession.close();
return list;
}
SqlSession sqlSession = sqlSessionFactory.openSession();
return sqlSession.selectOne("UserDaoImpl.findUserById", id);
SqlSession sqlSession = sqlSessionFactory.openSession();
return sqlSession.selectList("UserDaoImpl.findUserByName", user);
sqlSession.insert("UserDaoImpl.addUser",user);
SqlSession sqlSession = sqlSessionFactory.openSession();
int update = sqlSession.update("UserDaoImpl.updateUser",user);
事务提交
return update;
SqlSession sqlSession = sqlSessionFactory.openSession();
sqlSession.delete("UserDaoImpl.deleteUser",id);
事务提交
与动态代理的测试类差不多,只是略有不同而已。
private SqlSessionFactory sessionFactory;
@Before
public void init() throws IOException {
//创建SqlSessionFactoryBuilder
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
//解析核心配置文件
InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
sessionFactory = sqlSessionFactoryBuilder.build(is);
}
UserDaoImpl userDao = new UserDaoImpl(sessionFactory);
//调用方法
List<User> list=userDao.findAll();
for (User user : list) {
System.out.println(user);
}
只需要实现UserDaoImpl对象,并将sessionFactory传入即可。
<resultMap id="userResultMap" type="cn.domain.User">
<id property="对象中的属性" column="字段名称">
<result property=”对象中的属性” column=”字段名称”>
resultMap>
<mappers>
<package name="com.dao">package>
mappers>