mybatis增删改查快速实现!!!
生活随笔
收集整理的這篇文章主要介紹了
mybatis增删改查快速实现!!!
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Mybatis
簡介
**
1.什么是Mybatis
**
MyBatis是一款優(yōu)秀的基于java的持久層框架,它內(nèi)部
封裝了jdbc,使開發(fā)者只需要關(guān)注sql語句本身。
參考文檔 :https://mybatis.org/mybatis-3/zh/index.html
2.持久化
數(shù)據(jù)持久化
·持久化就是將程序的數(shù)據(jù)在持久狀態(tài)和瞬時狀態(tài)轉(zhuǎn)換的過程
·內(nèi)存:斷電即失
·數(shù)據(jù)庫(jdbc),io文件持久化
·生活中遇到的持久化:買的肉放時間太長會壞,所以我們買的肉要放到冰箱里冷藏,等吃了在去解凍。
3.持久層
Dao層,Service層,Controller層…
·完成持久化工作的代碼塊
·層界十分明
CRUD
1.搭建環(huán)境
- [ ] 1.搭建數(shù)據(jù)庫
CREATE DATABASE mybatis;USE mybatis;CREATE TABLE user( id int(20) NOT NULL PRIMARY KEY, name varchar(30) DEFAULT NULL, pwd varchar(20) DEFAULT NULL );INSERT INTO user(id,name,pwd) VALUES (1,'李李李','123456'), (2,'華晨宇','666666'), (3,'苑苑苑','520520');2.pom.xml
·新建一個普通的maven項目
·刪除src目錄
3.導(dǎo)入maven依賴 pom.xml
<dependencies><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.6</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.18</version></dependency> </dependencies>3.CRUD
4.創(chuàng)建一個模塊
·編寫mybatis的核心配置文件 mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configurationPUBLIC "-//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"/> <!-- ?useSSL=false--><property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false"/><property name="username" value="root"/><property name="password" value="123456"/></dataSource></environment></environments><mappers><mapper resource="UserDao.xml"/></mappers> </configuration>·編寫mybatis工具類 MybatisUtils
public class MybatisUtils {//使用mybatis的第一步:獲取sqlSessionFactory對象// private static SqlSessionFactory factory = null;private static SqlSessionFactory sqlSession;static {try {//使用mybatis第一步、獲取sqlSessionFactory對象String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);sqlSession = new SqlSessionFactoryBuilder().build(inputStream);} catch (IOException e) {e.printStackTrace();}}public static SqlSession getSqlSession(){return sqlSession.openSession();} }編寫代碼
·實體類 User
import lombok.Data;@Data public class User {private Integer id;private String name;private String pwd;public User() {}public User(Integer id, String name, String pwd) {this.id = id;this.name = name;this.pwd = pwd;}@Overridepublic String toString() {return "User{" +"id=" + id +", name='" + name + '\'' +", pwd='" + pwd + '\'' +'}';} }·Dao接口 UserDao
//這個類操作對象的實體 package com.guohui.dao; import com.guohui.pojo.User; import java.util.List;public interface UserDao {//查詢所有用戶List<User> getUserList();//根據(jù)id查詢用戶User getUserById(int id);//添加一個用戶int addUser(User user);//修改用戶int updateUser(User user);//根據(jù)id刪除用戶int deleteUser(int id); }·接口實現(xiàn)類 UserDao.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!--namespace=綁定對應(yīng)的Dao/mapper接口--> <mapper namespace="com.guohui.dao.UserDao"><!--select查詢語句--><select id="getUserList" resultType="com.guohui.pojo.User">select * from user</select> <!-- 根據(jù)id查詢用戶--><select id="getUserById" parameterType="int" resultType="com.guohui.pojo.User">select * from user where id = #{id}</select> <!-- 添加一個用戶--><insert id="addUser" parameterType="com.guohui.pojo.User">insert into user(id, name, pwd) values (#{id},#{name},#{pwd})</insert> <!-- 修改用戶--><update id="updateUser" parameterType="com.guohui.pojo.User">update user set name=#{name},pwd=#{pwd} where id = #{id}</update> <!-- 刪除用戶--><delete id="deleteUser" parameterType="int">delete from user where id = #{id}</delete> </mapper>·測試.junit測試
public class MyTest {@Testpublic void test(){//查詢?nèi)坑脩?/第一步:獲得SqlSession對象SqlSession sqlSession = MybatisUtils.getSqlSession();//執(zhí)行SQLUserDao mapper = sqlSession.getMapper(UserDao.class);List<User> userList = mapper.getUserList();for (User user : userList) {System.out.println(user);}//關(guān)閉SqlSessionsqlSession.close();}@Test//查詢id用戶public void test01(){//根據(jù)id查詢用戶SqlSession sqlSession = MybatisUtils.getSqlSession();UserDao mapper = sqlSession.getMapper(UserDao.class);//查詢id為1 的用戶User user = mapper.getUserById(1);System.out.println(user);sqlSession.close();}@Test//添加用戶public void addUser(){//添加一個用戶SqlSession sqlSession = MybatisUtils.getSqlSession();UserDao mapper = sqlSession.getMapper(UserDao.class);User user = new User(9,"苑雪兒","guohui520");int i = mapper.addUser(user);System.out.println("插入成功"+i);sqlSession.commit(); //提交事務(wù),重點!不寫的話不會提交到數(shù)據(jù)庫sqlSession.close();}@Test//修改用戶public void updateUser(){//添加一個用戶SqlSession sqlSession = MybatisUtils.getSqlSession();UserDao mapper = sqlSession.getMapper(UserDao.class);mapper.updateUser(new User(5,"李國輝","520yuanxueer"));System.out.println("修改成功!");sqlSession.commit(); //提交事務(wù),重點!不寫的話不會提交到數(shù)據(jù)庫sqlSession.close();}@Test//刪除用戶public void testDeleteUser() {SqlSession session = MybatisUtils.getSqlSession();UserDao mapper = session.getMapper(UserDao.class);int i = mapper.deleteUser(5);System.out.println(i);session.commit(); //提交事務(wù),重點!不寫的話不會提交到數(shù)據(jù)庫session.close();} }謝謝觀看,哪里寫的有問題可以評論,隨時在線修改。
總結(jié)
以上是生活随笔為你收集整理的mybatis增删改查快速实现!!!的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: idea文件头信息设置
- 下一篇: springboot创建项目(通过spr