您好,登錄后才能下訂單哦!
這篇文章主要介紹了Mybatis如何實現增刪改查,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
Mybatis實現增刪改查
在lib文件夾下面添加mybatis的核心jar包以及依賴的jar包
同在lib文件夾下面加入mysql的驅動jar包
<?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> <!-- 配置 properties文件--> <properties resource="db.properties"></properties> <settings> <!-- 配置日志的輸出形式 --> <setting name="logImpl" value="LOG4J" /> </settings> <!-- 配置數據庫的連接 默認使用哪一個數據庫連接 --> <environments default="mysql"> <!-- 配置mysql --> <environment id="mysql"> <!-- 事務管理 使用JDBC --> <transactionManager type="JDBC"></transactionManager> <!-- type 連接屬性 UNPOOLED-這個類型的數據源實現只是在每次需要的時候簡單地打開和關閉連接。 POOLED-這個數據源的實現緩存了JDBC 連接對象,用于避免每次創建新的數據庫連接時都初始 化和進行認證,加快程序響應。并發WEB應用通常通過這種做法來獲得快速響應。 NDI- 這個數據源的配置是為了準備與像gpring或應用服務器能夠在外部或者內部配置數據 源的容器一起使用,然后在NDI 上下文中引用它 --> <dataSource type="POOLED"> <property name="driver" value="${diver}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> </dataSource> </environment> </environments> <!-- 配置映射 --> <mappers> <mapper resource="com/sxt/mapping/UserMapper.xml" /> </mappers> </configuration>
注意:在設計數據庫時,id的類型為int并且設置為主鍵。自動遞增。
package com.sxt.domain; public class User { private Integer id; private String name; private String address; private String sex; public User(){ } //插入 public User(String name, String address, String sex) { super(); this.name = name; this.address = address; this.sex = sex; } //查詢 public User(Integer id, String name, String address, String sex) { super(); this.id = id; this.name = name; this.address = address; this.sex = sex; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", address=" + address + ", sex=" + sex + "]"; } }
package com.sxt.mapper; import com.sxt.domain.User; import java.util.List; public interface UserMapper { public void add(User user); public void update(User user); public void delete(Integer id); //刪除的構造方法 public User queryById(Integer id);//查詢的構造方法 public List<User> queryAll(); public List<User> queryLike(User user); //模糊查詢 }
<?xml version="1.0" encoding="UTF-8"?> <!-- 引入頭文件 --> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.sxt.mapper.UserMapper"> <!-- namespace一般與Mapper接口的限定名一致 --> <!--添加 id對應接口里面的方法名 parameterType參數類型 --> <insert id="add" parameterType="com.sxt.domain.User"> insert into user (name,address,sex) values(#{name},#{address},#{sex}) </insert> <!-- 修改 --> <update id="update" parameterType="com.sxt.domain.User"> update user set name=#{name},address=#{address},sex=#{sex} where id=#{id} </update> <!-- 刪除 --> <delete id="delete" parameterType="java.lang.Integer"> delete form user where id=#{value} </delete> <!--查詢一個 resultType返回值的類型 --> <select id="queryById" parameterType="java.lang.Integer" resultType="com.sxt.domain.User"> select * from user where id=#{value} </select> <!-- 全查詢 --> <select id="queryLike" resultType="com.sxt.domain.User"> <!-- select * from user where name like #{name} --> select * from user where name like "%"#{name}"%" </select> <!-- 模糊查詢 --> </mapper>
package com.sxt.utils; import java.io.InputStream; import javax.websocket.Session; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class MybatisUtils { static InputStream is=MybatisUtils.class.getResourceAsStream("/mybatis.xml"); //得到SqlSessionFactory static SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is); //得到session public static SqlSession openSession() { return factory.openSession(); } //關閉session public static void closeSession(SqlSession session) { //提交 session.commit(); //關閉 session.close(); } }
package com.sxt.text; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import com.sxt.domain.User; import com.sxt.mapper.UserMapper; import com.sxt.utils.MybatisUtils; public class mybatisTest { public static void main(String[] args) { SqlSession session=MybatisUtils.openSession(); UserMapper userMapper=session.getMapper(UserMapper.class); // TODO Auto-generated method stub User user=new User("小花", "武漢", "男"); userMapper.add(user); System.out.println("chen"); MybatisUtils.closeSession(session); }
package com.sxt.text; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import com.sxt.domain.User; import com.sxt.mapper.UserMapper; import com.sxt.utils.MybatisUtils; public class mybatisTest { // SqlSession session=MybatisUtils.openSession(); // UserMapper userMapper=session.getMapper(UserMapper.class); // @Test // public void initData(){ public static void main(String[] args) { SqlSession session=MybatisUtils.openSession(); UserMapper userMapper=session.getMapper(UserMapper.class); // TODO Auto-generated method stub // User user=new User("小明", "武漢", "男"); User user=new User(2,"小花","河北","女"); userMapper.update(user); MybatisUtils.closeSession(session); }
package com.sxt.text; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import com.sxt.domain.User; import com.sxt.mapper.UserMapper; import com.sxt.utils.MybatisUtils; public class mybatisTest { // SqlSession session=MybatisUtils.openSession(); // UserMapper userMapper=session.getMapper(UserMapper.class); // @Test // public void initData(){ public static void main(String[] args) { SqlSession session=MybatisUtils.openSession(); UserMapper userMapper=session.getMapper(UserMapper.class); // TODO Auto-generated method stub // User user=new User("小明", "武漢", "男"); //User user=new User(2,"小花","河北","女"); List<User> list=userMapper.queryAll(); System.out.println(list); MybatisUtils.closeSession(session); }
package com.sxt.text; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import com.sxt.domain.User; import com.sxt.mapper.UserMapper; import com.sxt.utils.MybatisUtils; public class mybatisTest { // SqlSession session=MybatisUtils.openSession(); // UserMapper userMapper=session.getMapper(UserMapper.class); // @Test // public void initData(){ public static void main(String[] args) { SqlSession session=MybatisUtils.openSession(); UserMapper userMapper=session.getMapper(UserMapper.class); // TODO Auto-generated method stub // User user=new User("小明", "武漢", "男"); //User user=new User(2,"小花","河北","女"); // List<User> list=userMapper.queryAll(); //System.out.println(list); userMapper.delete(3); MybatisUtils.closeSession(session); }
感謝你能夠認真閱讀完這篇文章,希望小編分享的“Mybatis如何實現增刪改查”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。