标签:
本篇将实现增删改功能,因为查询在上篇已经实现了。
1.实现数据的“增”功能:
在UserDao.java文件中新增一个方法:
/** * 新增一个用户 * @param user */ public void addUser(User user);
然后在UserMapper.xml中配置好这个方法的sql:
<!-- 新增一个user --> <!-- seGeneratedKeys设置为"true"表明要MyBatis获取由数据库自动生成的主键;keyProperty="id"指定把获取到的主键值注入到Student的id属性 --> <insert id="addUser" parameterType="user" useGeneratedKeys="true" keyProperty="id"> insert into t_user(user_name,user_age,user_address) values(#{userName},#{userAge},#{userAddress}) </insert>
然后在UserTest.java类中测试这个方法:
package cn.clear.mybatis.test; import java.io.IOException; import java.io.Reader; import java.util.List; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import cn.clear.mybatis.dao.UserDao; import cn.clear.mybatis.model.Article; import cn.clear.mybatis.model.User; /** * 测试mybatis * @author clear * */ public class UserTest { //定义sessionFactory属性 private static SqlSessionFactory sessionFactory; private static Reader reader; static{ try { //读取配置文件信息 reader = Resources.getResourceAsReader("configuration.xml"); //构建sessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { addUser(); findUserByIdAndFindAllUser(); } public static void findUserByIdAndFindAllUser() { SqlSession session = sessionFactory.openSession(); try{ //使用原始selectOne查询 //User user = session.selectOne("cn.clear.mybatis.model.UserMapper.selectUserById",1); //使用dao接口查询 UserDao userDao = session.getMapper(UserDao.class); User user = userDao.selectUserById(1); System.out.println(user.getId()+" "+user.getUserName()+" "+user.getUserAge()+" "+user.getUserAddress()); System.out.println("------------------------感谢华丽丽的分割线-----------------------------"); List<User> userList = userDao.selectAllUser(); for (User u : userList) { System.out.println(u.getId()+" "+u.getUserName()+" "+u.getUserAge()+" "+u.getUserAddress()); } }finally{ session.close(); } } public static void addUser(){ User user = new User(); user.setUserName("小三"); user.setUserAge(25); user.setUserAddress("中国广州天河体育中心"); SqlSession session = sessionFactory.openSession(); try { UserDao userDao = session.getMapper(UserDao.class); userDao.addUser(user); session.commit(); System.out.println("当前新增用户id为:"+user.getId()); } finally { session.close(); } } }
运行测试类,console打印:
当前新增用户id为:3 1 小子 26 广州天河万菱国际中心 ------------------------感谢华丽丽的分割线----------------------------- 3 小三 25 中国广州天河体育中心 2 小二 27 中国广州天河体育中心 1 小子 26 广州天河万菱国际中心
于是,我们成功地将增功能实现了。
下面实现修改(update)功能:
在UserDao.java中新增方法:
/** * 更新用户 * @param user */ public void updateUser(User user);
在UserMapper.xml中配置改方法的sql:
<!-- 更新用户 --> <update id="updateUser" parameterType="user"> update t_user set user_name=#{userName},user_age=#{userAge},user_address=#{userAddress} where id=#{id} </update>
在UserTest.java中写测试方法:
package cn.clear.mybatis.test; import java.io.IOException; import java.io.Reader; import java.util.List; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import cn.clear.mybatis.dao.UserDao; import cn.clear.mybatis.model.Article; import cn.clear.mybatis.model.User; /** * 测试mybatis * @author clear * */ public class UserTest { //定义sessionFactory属性 private static SqlSessionFactory sessionFactory; private static Reader reader; static{ try { //读取配置文件信息 reader = Resources.getResourceAsReader("configuration.xml"); //构建sessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { //addUser(); update(); findUserByIdAndFindAllUser(); //delete(); //getUserArticle(); } public static void findUserByIdAndFindAllUser() { SqlSession session = sessionFactory.openSession(); try{ //使用原始selectOne查询 //User user = session.selectOne("cn.clear.mybatis.model.UserMapper.selectUserById",1); //使用dao接口查询 UserDao userDao = session.getMapper(UserDao.class); User user = userDao.selectUserById(1); System.out.println(user.getId()+" "+user.getUserName()+" "+user.getUserAge()+" "+user.getUserAddress()); System.out.println("------------------------感谢华丽丽的分割线-----------------------------"); List<User> userList = userDao.selectAllUser(); for (User u : userList) { System.out.println(u.getId()+" "+u.getUserName()+" "+u.getUserAge()+" "+u.getUserAddress()); } }finally{ session.close(); } } public static void addUser(){ User user = new User(); user.setUserName("小三"); user.setUserAge(25); user.setUserAddress("中国广州天河体育中心"); SqlSession session = sessionFactory.openSession(); try { UserDao userDao = session.getMapper(UserDao.class); userDao.addUser(user); session.commit(); System.out.println("当前新增用户id为:"+user.getId()); } finally { session.close(); } } public static void update(){ SqlSession session = sessionFactory.openSession(); try{ UserDao userDao = session.getMapper(UserDao.class); User u = userDao.selectUserById(2); u.setUserName("小二"); u.setUserAddress("广州市天河区正佳广场"); userDao.updateUser(u); session.commit(); }finally{ session.close(); } } }
运行测试类,console打印:
1 小子 26 广州天河万菱国际中心 ------------------------感谢华丽丽的分割线----------------------------- 3 小三 25 中国广州天河体育中心 2 小二 27 广州市天河区正佳广场 1 小子 26 广州天河万菱国际中心
update成功。
下面实现删功能(delete)。
在UserDao中增加一个方法:
/** * 删除用户 * @param id */ public void deleteUser(Integer id);
在UserMapper.xml中配置sql:
<!-- 删除用户 --> <delete id="deleteUser" parameterType="int"> delete from t_user where id=#{id} </delete>
在UserTest.java类中增加测试方法:
package cn.clear.mybatis.test; import java.io.IOException; import java.io.Reader; import java.util.List; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import cn.clear.mybatis.dao.UserDao; import cn.clear.mybatis.model.Article; import cn.clear.mybatis.model.User; /** * 测试mybatis * @author clear * */ public class UserTest { //定义sessionFactory属性 private static SqlSessionFactory sessionFactory; private static Reader reader; static{ try { //读取配置文件信息 reader = Resources.getResourceAsReader("configuration.xml"); //构建sessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { //addUser(); //update(); delete(); findUserByIdAndFindAllUser(); } public static void findUserByIdAndFindAllUser() { SqlSession session = sessionFactory.openSession(); try{ //使用原始selectOne查询 //User user = session.selectOne("cn.clear.mybatis.model.UserMapper.selectUserById",1); //使用dao接口查询 UserDao userDao = session.getMapper(UserDao.class); User user = userDao.selectUserById(1); System.out.println(user.getId()+" "+user.getUserName()+" "+user.getUserAge()+" "+user.getUserAddress()); System.out.println("------------------------感谢华丽丽的分割线-----------------------------"); List<User> userList = userDao.selectAllUser(); for (User u : userList) { System.out.println(u.getId()+" "+u.getUserName()+" "+u.getUserAge()+" "+u.getUserAddress()); } }finally{ session.close(); } } public static void addUser(){ User user = new User(); user.setUserName("小三"); user.setUserAge(25); user.setUserAddress("中国广州天河体育中心"); SqlSession session = sessionFactory.openSession(); try { UserDao userDao = session.getMapper(UserDao.class); userDao.addUser(user); session.commit(); System.out.println("当前新增用户id为:"+user.getId()); } finally { session.close(); } } public static void update(){ SqlSession session = sessionFactory.openSession(); try{ UserDao userDao = session.getMapper(UserDao.class); User u = userDao.selectUserById(2); u.setUserName("小二"); u.setUserAddress("广州市天河区正佳广场"); userDao.updateUser(u); session.commit(); }finally{ session.close(); } } public static void delete(){ SqlSession session = sessionFactory.openSession(); try{ UserDao userDao = session.getMapper(UserDao.class); userDao.deleteUser(3); session.commit(); }finally{ session.close(); } } }
运行测试类,console打印:
1 小子 26 广州天河万菱国际中心 ------------------------感谢华丽丽的分割线----------------------------- 2 小二 27 广州市天河区正佳广场 1 小子 26 广州天河万菱国际中心
So,完成。
标签:
原文地址:http://www.cnblogs.com/clear5/p/5001118.html