mybatis完成對數據的增刪查改實例詳解。本站提示廣大學習愛好者:(mybatis完成對數據的增刪查改實例詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是mybatis完成對數據的增刪查改實例詳解正文
後期預備
新建java工程或java wweb工程,須要導入以下的包,
根本任務曾經完成,接上去開端進入正題。
新建實體類
新建與數據庫表對應的實體類
package com.edu.hpu.domain; /** * @author Administrator *user表所對應的實體類 */ public class User { //實體類的屬性和表的字段稱號逐個對應 private int id; private String name; private int age; //對屬性停止封裝 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } //添加toString辦法 @Override public String toString() { return "User [id=" + id + ",name=" + name + ",age=" + age + "]"; } }
添加Mybatis對象類
添加用到的對象類,以下所示,
package com.edu.hpu.util; import java.io.InputStream; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class MyBatisUtil { /** * 獲得SqlSessionFactory * @return SqlSessionFactory */ public static SqlSessionFactory getSqlSessionFactory() { String resource = "conf.xml";//調取設置裝備擺設文件 InputStream is = MyBatisUtil.class.getClassLoader().getResourceAsStream(resource); SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is); return factory; } /** * 獲得SqlSession * @return SqlSession */ public static SqlSession getSqlSession() { return getSqlSessionFactory().openSession(); } /** * 獲得SqlSession * @param isAutoCommit * true 表現創立的SqlSession對象在履行完SQL以後會主動提交事務 * false 表現創立的SqlSession對象在履行完SQL以後不會主動提交事務,這時候就須要我們手動挪用sqlSession.commit()提交事務 * @return SqlSession */ public static SqlSession getSqlSession(boolean isAutoCommit) { return getSqlSessionFactory().openSession(isAutoCommit); } }
應用mybatis對數據停止增刪查改操作有兩種辦法,分離為設置裝備擺設文件操作和注解操作。
經由過程設置裝備擺設文件停止操作
數據庫設置裝備擺設文件以下所示,對數據庫信息停止設置裝備擺設,
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <!-- 設置裝備擺設數據庫銜接信息 --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull" /> <property name="username" value="root" /> <property name="password" value="admin" /> </dataSource> </environment> </environments> <mappers> <!-- 注冊userMapper.xml文件, userMapper.xml位於com.edu.hpu.mapping這個包下,所以resource寫成com/edu/hpu/mapping/userMapper.xml--> <mapper resource="com/edu/hpu/mapping/userMapper.xml"/> <!-- <mapper class="com.edu.hpu.mapping.UserMapper_11"/> --> </mappers> </configuration>
設置裝備擺設操作數據庫語句文件,以下所示,
<?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="com.edu.hpu.mapping.userMapper"> <!-- 在select標簽中編寫查詢的SQL語句, 設置select標簽的id屬性為getUser,id屬性值必需是獨一的,不克不及夠反復 應用parameterType屬性指明查詢時應用的參數類型,resultType屬性指明查詢前往的成果集類型 resultType="com.edu.hpu.domain.User"就表現將查詢成果封裝成一個User類的對象前往 User類就是users表所對應的實體類 --> <!-- 依據id查詢獲得一個user對象 --> <select id="getUser" parameterType="int" resultType="com.edu.hpu.domain.User"> select * from users where id=#{id} </select> <!-- 創立用戶(Create) --> <insert id="addUser" parameterType="com.edu.hpu.domain.User"> insert into users(name,age) values(#{name},#{age}) </insert> <!-- 刪除用戶(Remove) --> <delete id="deleteUser" parameterType="int"> delete from users where id=#{id} </delete> <!-- 修正用戶(Update) --> <update id="updateUser" parameterType="com.edu.hpu.domain.User"> update users set name=#{name},age=#{age} where id=#{id} </update> <!-- 查詢全體用戶--> <select id="getAllUsers" resultType="com.edu.hpu.domain.User"> select * from users </select> </mapper>
經由過程設置裝備擺設文件完成對數據庫的增刪查改根本上曾經完成,接上去給出測試類,
可以停止測試,以下所示,
package com.edu.hpu.test; import java.util.List; import com.edu.hpu.domain.User; import com.edu.hpu.util.MyBatisUtil; import org.junit.Test; import org.apache.ibatis.session.SqlSession; public class Test2 { @Test public void testAdd(){ //SqlSession sqlSession = MyBatisUtil.getSqlSession(false); SqlSession sqlSession = MyBatisUtil.getSqlSession(true); /** * 映照sql的標識字符串, * com.edu.hpu.mapping.userMapper是userMapper.xml文件中mapper標簽的namespace屬性的值, * addUser是insert標簽的id屬性值,經由過程insert標簽的id屬性值便可以找到要履行的SQL */ String statement = "com.edu.hpu.mapping.userMapper.addUser";//映照sql的標識字符串 User user = new User(); user.setName("新增用戶小黃"); user.setAge(20); //履行拔出操作 int retResult = sqlSession.insert(statement,user); //手動提交事務 //sqlSession.commit(); //應用SqlSession履行完SQL以後須要封閉SqlSession sqlSession.close(); System.out.println(retResult); } @Test public void testUpdate(){ SqlSession sqlSession = MyBatisUtil.getSqlSession(true); /** * 映照sql的標識字符串, * com.edu.hpu.mapping.userMapper是userMapper.xml文件中mapper標簽的namespace屬性的值, * updateUser是update標簽的id屬性值,經由過程update標簽的id屬性值便可以找到要履行的SQL */ String statement = "com.edu.hpu.mapping.userMapper.updateUser";//映照sql的標識字符串 User user = new User(); user.setId(3); user.setName("hello world"); user.setAge(25); //履行修正操作 int retResult = sqlSession.update(statement,user); //應用SqlSession履行完SQL以後須要封閉SqlSession sqlSession.close(); System.out.println(retResult); } @Test public void testDelete(){ SqlSession sqlSession = MyBatisUtil.getSqlSession(true); /** * 映照sql的標識字符串, * com.edu.hpu.mapping.userMapper是userMapper.xml文件中mapper標簽的namespace屬性的值, * deleteUser是delete標簽的id屬性值,經由過程delete標簽的id屬性值便可以找到要履行的SQL */ String statement = "com.edu.hpu.mapping.userMapper.deleteUser";//映照sql的標識字符串 //履行刪除操作 int retResult = sqlSession.delete(statement,4); //應用SqlSession履行完SQL以後須要封閉SqlSession sqlSession.close(); System.out.println(retResult); } @Test public void testGetAll(){ SqlSession sqlSession = MyBatisUtil.getSqlSession(); /** * 映照sql的標識字符串, * com.edu.hpu.mapping.userMapper是userMapper.xml文件中mapper標簽的namespace屬性的值, * getAllUsers是select標簽的id屬性值,經由過程select標簽的id屬性值便可以找到要履行的SQL */ String statement = "com.edu.hpu.mapping.userMapper.getAllUsers";//映照sql的標識字符串 //履行查詢操作,將查詢成果主動封裝成List<User>前往 List<User> lstUsers = sqlSession.selectList(statement); //應用SqlSession履行完SQL以後須要封閉SqlSession sqlSession.close(); System.out.println(lstUsers); } }
經由過程注解停止操作
經由過程注解停止操作須要寫一個接口,然則不用完成,以下所示,
package com.edu.hpu.mapping; import java.util.List; import com.edu.hpu.domain.User; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; /** * @author gacl * 界說sql映照的接口,應用注解指明辦法要履行的SQL */ public interface UserMapper_11 { //應用@Insert注解指明add辦法要履行的SQL @Insert("insert into users(name, age) values(#{name}, #{age})") public int add(User user); //應用@Delete注解指明deleteById辦法要履行的SQL @Delete("delete from users where id=#{id}") public int deleteById(int id); //應用@Update注解指明update辦法要履行的SQL @Update("update users set name=#{name},age=#{age} where id=#{id}") public int update(User user); //應用@Select注解指明getById辦法要履行的SQL @Select("select * from users where id=#{id}") public User getById(int id); //應用@Select注解指明getAll辦法要履行的SQL @Select("select * from users") public List<User> getAll(); }
同時,須要在數據庫設置裝備擺設文件中添加所寫的接口,在conf.xml中添加以下語句,
<mapper class="com.edu.hpu.mapping.UserMapper_11"/>
OK,根本上曾經完成,這裡給出測試類可以停止測試,
package com.edu.hpu.test; import java.util.List; import com.edu.hpu.domain.User; import com.edu.hpu.mapping.UserMapper_11; import com.edu.hpu.util.MyBatisUtil; import org.apache.ibatis.session.SqlSession; import org.junit.Test; /** * @author Administrator *對注解停止測試 */ public class Test3 { @Test public void testAdd(){ SqlSession sqlSession = MyBatisUtil.getSqlSession(true); //獲得UserMapper接口的完成類對象,UserMapper接口的完成類對象由sqlSession.getMapper(UserMapper.class)靜態構建出來 UserMapper_11 mapper = sqlSession.getMapper(UserMapper_11.class); User user = new User(); user.setName("年夜智若愚"); user.setAge(20); int add = mapper.add(user); //應用SqlSession履行完SQL以後須要封閉SqlSession sqlSession.close(); System.out.println(add); } @Test public void testUpdate(){ SqlSession sqlSession = MyBatisUtil.getSqlSession(true); //獲得UserMapper接口的完成類對象,UserMapper接口的完成類對象由sqlSession.getMapper(UserMapper.class)靜態構建出來 UserMapper_11 mapper = sqlSession.getMapper(UserMapper_11.class); User user = new User(); user.setId(3); user.setName("年夜音希聲"); user.setAge(26); //履行修正操作 int retResult = mapper.update(user); //應用SqlSession履行完SQL以後須要封閉SqlSession sqlSession.close(); System.out.println(retResult); } @Test public void testDelete(){ SqlSession sqlSession = MyBatisUtil.getSqlSession(true); //獲得UserMapper接口的完成類對象,UserMapper接口的完成類對象由sqlSession.getMapper(UserMapper.class)靜態構建出來 UserMapper_11 mapper = sqlSession.getMapper(UserMapper_11.class); //履行刪除操作 int retResult = mapper.deleteById(7); //應用SqlSession履行完SQL以後須要封閉SqlSession sqlSession.close(); System.out.println(retResult); } @Test public void testGetUser(){ SqlSession sqlSession = MyBatisUtil.getSqlSession(); //獲得UserMapper接口的完成類對象,UserMapperI接口的完成類對象由sqlSession.getMapper(UserMapper.class)靜態構建出來 UserMapper_11 mapper = sqlSession.getMapper(UserMapper_11.class); //履行查詢操作,將查詢成果主動封裝成User前往 User user = mapper.getById(1); //應用SqlSession履行完SQL以後須要封閉SqlSession sqlSession.close(); System.out.println(user); } @Test public void testGetAll(){ SqlSession sqlSession = MyBatisUtil.getSqlSession(); //獲得UserMapper接口的完成類對象,UserMapper接口的完成類對象由sqlSession.getMapper(UserMapper.class)靜態構建出來 UserMapper_11 mapper = sqlSession.getMapper(UserMapper_11.class); //履行查詢操作,將查詢成果主動封裝成List<User>前往 List<User> lstUsers = mapper.getAll(); //應用SqlSession履行完SQL以後須要封閉SqlSession sqlSession.close(); System.out.println(lstUsers); } }
以上所述是小編給年夜家引見的mybatis完成對數據的增刪查改實例詳解,願望對年夜家有所贊助,假如年夜家有任何疑問請給我留言,小編會實時答復年夜家的。在此也異常感激年夜家對網站的支撐!