如何通过MyBatis实现增删改
UserMapper.xml
[html] view plain copy
- <!--增加 -->
- <insert id="add" parameterType="User">
- insert into smbms_user(userCode,userName,userPassword,gender,birthday,phone,address,userRole,createdBy,creationDate)
- values(#{userCode},#{userName},#{userPassword},#{gender},#{birthday},#{phone},#{address},#{userRole},#{createdBy},#{creationDate})
- </insert>
- <!-- 修改(4个起参数用对象)-->
- <update id="updatee" parameterType="User">
- update smbms_user set userCode=#{userCode},userName=#{userName},userPassword=#{userPassword},
- gender=#{gender},birthday=#{birthday},phone=#{phone},address=#{address},userRole=#{userRole},modifyBy=#{modifyBy},modifyDate=#{modifyDate}
- where id=#{id}
- </update>
- <!-- 修改密码(1-3参数使用@param) -->
- <update id="updatePwd">
- update smbms_user set userPassword=#{userPassword} where id=#{id}
- </update>
- <!-- 删除 -->
- <delete id="deletee" parameterType="int">
- delete from smbms_user where id=#{id}
- </delete>
UserMapper.java
[html] view plain copy
- //增加
- public int add(User user);
- //修改
- public int updatee(User user);
- //修改密码
- public int updatePwd(@Param("id")Integer id,@Param("userPassword")String pwd);
- //删除
- public int deletee(Integer id);
MapperTest.java
[html] view plain copy
- //添加数据条
- @Test
- public void test9() {
- SqlSession sqlSession=null;
- int count=0;
- try {
- sqlSession=MyBatisUtil.createSqlSession();
- User user=new User();
- user.setUserCode("003");
- user.setUserName("用户0011");
- user.setUserPassword("1234561");
- //如何添加Date类型的生日日期
- Date birthday=new SimpleDateFormat("yyyy-MM-dd").parse("2000-11-11");
- user.setBirthday(birthday);
- //如何添加 创建日期
- user.setCreationDate(new Date());
- user.setAddress("南京1");
- user.setGender(1);
- user.setPhone("13367891214");
- user.setUserRole(1);
- user.setCreatedBy(1);
- count=sqlSession.getMapper(UserMapper.class).add(user);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally{
- MyBatisUtil.closeSqlSession(sqlSession);
- }
- System.out.println(count);
- }
- //修改某数据条中的3个以上字段对应的数据内容
- @Test
- public void test10() {
- SqlSession sqlSession=null;
- int count=0;
- try {
- sqlSession=MyBatisUtil.createSqlSession();
- User user=new User();
- user.setId(18);
- user.setUserCode("17");
- user.setUserName("用户00");
- user.setUserPassword("1234");
- Date birthday=new SimpleDateFormat("yyyy-MM-dd").parse("2000-11-11");
- user.setBirthday(birthday);
- user.setCreationDate(new Date());
- user.setAddress("南京12");
- user.setGender(1);
- user.setPhone("133678912344");
- user.setUserRole(2);
- //user.setCreatedBy(1);
- user.setModifyBy(1);
- user.setModifyDate(new Date());
- count=sqlSession.getMapper(UserMapper.class).updatee(user);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally{
- MyBatisUtil.closeSqlSession(sqlSession);
- }
- System.out.println(count);
- }
- //修改某数据条的3个之内字段对应的数据内容
- @Test
- public void test11() {
- SqlSession sqlSession=null;
- int count=0;
- //密码和 id两个字段
- String pwd="666888";
- Integer id=1;
- try {
- sqlSession=MyBatisUtil.createSqlSession();
- count=sqlSession.getMapper(UserMapper.class).updatePwd(id, pwd);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally{
- MyBatisUtil.closeSqlSession(sqlSession);
- }
- System.out.println(count);
- }
- //删除数据条
- @Test
- public void test12() {
- SqlSession sqlSession=null;
- int count=0;
- Integer id=19;
- try {
- sqlSession=MyBatisUtil.createSqlSession();
- count=sqlSession.getMapper(UserMapper.class).deletee(id);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally{
- MyBatisUtil.closeSqlSession(sqlSession);
- }
- System.out.println(count);
- }
相关推荐
xiuyangsong 2020-11-16
Nishinoshou 2020-11-09
jimgreatly 2020-09-01
dongxurr 2020-08-18
Dullonjiang 2020-08-15
Dullonjiang 2020-08-11
Dullonjiang 2020-08-09
dongxurr 2020-08-08
yunzhonmghe 2020-08-07
jimgreatly 2020-08-03
Dullonjiang 2020-07-30
jimgreatly 2020-07-27
liqiancao 2020-07-26
xiuyangsong 2020-07-26
dongxurr 2020-07-26
mcvsyy 2020-07-26
helloxusir 2020-07-25
牧场SZShepherd 2020-07-20