第二天:利用mybatis实现对数据库的增删查改
鸟瞰图:必须一一对应上!
以下上代码
1、insert
<insert id="addUser" parameterType="com.song.pojo.User">
insert into testweb.users (userName, password, sex, email)
values (#{userName},#{password},#{sex},#{email})
</insert>
2、delete
<delete id="deleteUserByID" parameterType="int">
delete from users where userId=#{id}
</delete>
3、select
<select id="getUserList" resultType="com.song.pojo.User">
select * from users
</select>
4、update;懒得写了太简单了哈哈
5、测试
public class test {
@Test
public void test(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserDao userDao=sqlSession.getMapper(UserDao.class);
List<User> userList = userDao.getUserList();
for (User user : userList) {
System.out.println(user);
}
}
@Test
public void add(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserDao mapper = sqlSession.getMapper(UserDao.class);
mapper.addUser(new User(5,"吴亦凡","123456","男","wyfqq.com"));
sqlSession.commit();
sqlSession.close();
}
@Test
public void deldte(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserDao mapper = sqlSession.getMapper(UserDao.class);
mapper.deleteUserByID(5);
sqlSession.commit();
sqlSession.close();
}
}
重点:
增删改需要提交事务