新建maven项目
在IDEA里新建一个maven
项目。依赖里下载相关的包
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>mybatis</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.complier.source>11</maven.complier.source> <maven.complier.target>11</maven.complier.target> </properties> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.29</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> </dependencies> </project>
接着在java 里面 新建一个包 里面新建一个User类 定义一个实体类
并输出se
t 和get
方法 快捷键alt+ins
在resources目录下创建一个mapper
包 用来配置UserMapper.xml
文件
并且创建 db.properties
和 mybatis-config.xml
UserMapper.xml
<?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="cn.wxz.mybatistest.pojo.User"> <select id="findUserById" parameterType="int" resultType="cn.wxz.mybatistest.pojo.User"> select * from user where uid = #{uid} </select> <insert id="addUser" parameterType="cn.wxz.mybatistest.pojo.User"> insert into user (uname,uage)values(#{uname},#{uage}); </insert> <update id="updateUser" parameterType="cn.wxz.mybatistest.pojo.User"> update user set uname=#{uname},uage=#{uage} where uid=#{uid}; </update> <delete id="deleteUser" parameterType="int"> delete from user where uid=#{uid}; </delete> </mapper>
db.properties
driver=com.mysql.cj.jdbc.Driver //端口号默认3306 url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC username=root //账号 password=root //密码
mybatis-config.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 resource="db.properties"/> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="mapper/UserMapper.xml"/> </mappers> </configuration>
然后打开mysql 创建一个user表
DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `uid` int(0) NOT NULL AUTO_INCREMENT, `uname` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL, `uage` int(0) NOT NULL, PRIMARY KEY (`uid`) USING BTREE )
接着创建一个UserTest
类 实现我们的增删改查
addUserTest
@Test public void addUserTest() { String resouces="mybatis-config.xml"; Reader reader=null; try { reader= Resources.getResourceAsReader(resouces); } catch (IOException e) { e.printStackTrace(); } SqlSessionFactory sqlMapper=new SqlSessionFactoryBuilder().build(reader); SqlSession sqlSession=sqlMapper.openSession(); User user=new User(); user.setUname("王五"); user.setUage(19); int ret = sqlSession.insert("addUser",user); sqlSession.commit(); if(ret>0) System.out.println("添加成功!!"); else System.out.println("添加失败!!"); sqlSession.close(); }
然后我们 查看当前user表
输入
select * from user
updateUserTest
@Test public void updateUserTest() { String resouces="mybatis-config.xml"; Reader reader=null; try { reader= Resources.getResourceAsReader(resouces); } catch (IOException e) { e.printStackTrace(); } SqlSessionFactory sqlMapper=new SqlSessionFactoryBuilder().build(reader); SqlSession sqlSession=sqlMapper.openSession(); User user=new User(); user.setUid(1); user.setUname("小王同学"); user.setUage(12); int ret=sqlSession.update("updateUser",user); if(ret>0) System.out.println("修改成功"); else System.out.println("修改失败"); sqlSession.commit(); sqlSession.close(); }
原来的张三变成了小王同学 uage从原来的19变成12了
成功!~
表里多出了一条数据
操作成功
findUserById
@Test public void findUserById(){ String resouces="mybatis-config.xml"; Reader reader=null; try { reader= Resources.getResourceAsReader(resouces); } catch (IOException e) { e.printStackTrace(); } SqlSessionFactory sqlMapper=new SqlSessionFactoryBuilder().build(reader); SqlSession sqlSession=sqlMapper.openSession(); User user=sqlSession.selectOne("findUserById",2); System.out.println(user.getUname()); sqlSession.close(); }
uid 为2 的时候 对应的uname是李四
成功!~
deleteUserTest
@Test public void deleteUserTest() { String resouces="mybatis-config.xml"; Reader reader=null; try { reader= Resources.getResourceAsReader(resouces); } catch (IOException e) { e.printStackTrace(); } SqlSessionFactory sqlMapper=new SqlSessionFactoryBuilder().build(reader); SqlSession sqlSession=sqlMapper.openSession(); int result=sqlSession.delete("deleteUser",1); if(result>0) System.out.println("删除成功"); else System.out.println("删除失败!"); sqlSession.commit(); sqlSession.close(); }
原来的uid为1 的小王同学的一列删除成功!!~
完整代码附上:
package cn.wxz.mybatistest.test; import cn.wxz.mybatistest.pojo.User; 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 org.junit.Test; import java.io.IOException; import java.io.Reader; public class UserTest { @Test public void findUserById(){ String resouces="mybatis-config.xml"; Reader reader=null; try { reader= Resources.getResourceAsReader(resouces); } catch (IOException e) { e.printStackTrace(); } SqlSessionFactory sqlMapper=new SqlSessionFactoryBuilder().build(reader); SqlSession sqlSession=sqlMapper.openSession(); User user=sqlSession.selectOne("findUserById",2); System.out.println(user.getUname()); sqlSession.close(); } @Test public void addUserTest() { String resouces="mybatis-config.xml"; Reader reader=null; try { reader= Resources.getResourceAsReader(resouces); } catch (IOException e) { e.printStackTrace(); } SqlSessionFactory sqlMapper=new SqlSessionFactoryBuilder().build(reader); SqlSession sqlSession=sqlMapper.openSession(); User user=new User(); user.setUname("王五"); user.setUage(19); int ret = sqlSession.insert("addUser",user); sqlSession.commit(); if(ret>0) System.out.println("添加成功!!"); else System.out.println("添加失败!!"); sqlSession.close(); } @Test public void updateUserTest() { String resouces="mybatis-config.xml"; Reader reader=null; try { reader= Resources.getResourceAsReader(resouces); } catch (IOException e) { e.printStackTrace(); } SqlSessionFactory sqlMapper=new SqlSessionFactoryBuilder().build(reader); SqlSession sqlSession=sqlMapper.openSession(); User user=new User(); user.setUid(1); user.setUname("小王同学"); user.setUage(12); int ret=sqlSession.update("updateUser",user); if(ret>0) System.out.println("修改成功"); else System.out.println("修改失败"); sqlSession.commit(); sqlSession.close(); } @Test public void deleteUserTest() { String resouces="mybatis-config.xml"; Reader reader=null; try { reader= Resources.getResourceAsReader(resouces); } catch (IOException e) { e.printStackTrace(); } SqlSessionFactory sqlMapper=new SqlSessionFactoryBuilder().build(reader); SqlSession sqlSession=sqlMapper.openSession(); int result=sqlSession.delete("deleteUser",1); if(result>0) System.out.println("删除成功"); else System.out.println("删除失败!"); sqlSession.commit(); sqlSession.close(); } }
以上就是王同学给大家带来的mybatis连接mysql 实现CRUD!