Mybatis實現增刪改查
Mybatis實現增刪改查
一.mybatis的配置
1.1 添加相應的jar包
在lib文件夾下面添加mybatis的核心jar包以及依賴的jar包
同在lib文件夾下面加入mysql的驅動jar包
1.2 配置mybatis.xml文件
<?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>
1.3 創建數據庫
註意:在設計數據庫時,id的類型為int並且設置為主鍵。自動遞增。
1.4 創建實體類
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 + "]"; } }
1.5 創建接口實現的方法
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); //模糊查詢 }
1.6 配置UserMapper.xml 文件
<?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>
1.7 創建MybatisUtils的方法
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(); } }
二、Mybatis的增刪改查
2.1 添加
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); }
2.2 修改
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); }
2.3 查詢
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); }
2.4 刪除
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實現增刪改查的文章就介紹到這瞭,更多相關Mybatis增刪改查內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- 淺談MyBatis執行SQL的兩種方式
- Mybatis CURD及模糊查詢功能的實現
- Mybatis結果集映射與生命周期詳細介紹
- Mybatis註解增刪改查的實例代碼
- 關於mybatis的一級緩存和二級緩存的那些事兒