0


mybatis 连接mysql 实现CRUD

在这里插入图片描述
在这里插入图片描述

💟💟前言

友友们大家好,我是你们的小王同学😗😗
今天给大家打来的是 mybatis 连接mysql 实现CRUD
希望能给大家带来有用的知识
觉得小王写的不错的话麻烦动动小手 点赞👍 收藏⭐ 评论📄
小王的主页:小王同学🚗
小王的gitee:小王同学🏩🏩
小王的github:小王同学💦

新建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表

DROPTABLEIFEXISTS`user`;CREATETABLE`user`(`uid`int(0)NOTNULLAUTO_INCREMENT,`uname`varchar(20)CHARACTERSET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOTNULL,`uage`int(0)NOTNULL,PRIMARYKEY(`uid`)USINGBTREE)

在这里插入图片描述
**接着创建一个

UserTest

类 实现我们的增删改查**

addUserTest

@TestpublicvoidaddUserTest(){String  resouces="mybatis-config.xml";Reader reader=null;try{
            reader=Resources.getResourceAsReader(resouces);}catch(IOException e){
            e.printStackTrace();}SqlSessionFactory sqlMapper=newSqlSessionFactoryBuilder().build(reader);SqlSession sqlSession=sqlMapper.openSession();User user=newUser();
        user.setUname("王五");
        user.setUage(19);int ret = sqlSession.insert("addUser",user);
        sqlSession.commit();if(ret>0)System.out.println("添加成功!!");elseSystem.out.println("添加失败!!");
        

        sqlSession.close();}

在这里插入图片描述然后我们 查看当前user表
输入

select * from user

在这里插入图片描述

updateUserTest

@TestpublicvoidupdateUserTest(){String  resouces="mybatis-config.xml";Reader reader=null;try{
            reader=Resources.getResourceAsReader(resouces);}catch(IOException e){
            e.printStackTrace();}SqlSessionFactory sqlMapper=newSqlSessionFactoryBuilder().build(reader);SqlSession sqlSession=sqlMapper.openSession();User user=newUser();
        user.setUid(1);
        user.setUname("小王同学");
        user.setUage(12);int ret=sqlSession.update("updateUser",user);if(ret>0)System.out.println("修改成功");elseSystem.out.println("修改失败");

        sqlSession.commit();
        sqlSession.close();}

在这里插入图片描述
在这里插入图片描述
原来的张三变成了小王同学 uage从原来的19变成12了
成功!~

表里多出了一条数据
操作成功

findUserById

@TestpublicvoidfindUserById(){String  resouces="mybatis-config.xml";Reader reader=null;try{
            reader=Resources.getResourceAsReader(resouces);}catch(IOException e){
            e.printStackTrace();}SqlSessionFactory sqlMapper=newSqlSessionFactoryBuilder().build(reader);SqlSession sqlSession=sqlMapper.openSession();User user=sqlSession.selectOne("findUserById",2);System.out.println(user.getUname());

        sqlSession.close();}

在这里插入图片描述
uid 为2 的时候 对应的uname是李四
成功!~

deleteUserTest

@TestpublicvoiddeleteUserTest(){String  resouces="mybatis-config.xml";Reader reader=null;try{
            reader=Resources.getResourceAsReader(resouces);}catch(IOException e){
            e.printStackTrace();}SqlSessionFactory sqlMapper=newSqlSessionFactoryBuilder().build(reader);SqlSession sqlSession=sqlMapper.openSession();int result=sqlSession.delete("deleteUser",1);if(result>0)System.out.println("删除成功");elseSystem.out.println("删除失败!");

        sqlSession.commit();
        sqlSession.close();}

在这里插入图片描述
在这里插入图片描述
原来的uid为1 的小王同学的一列删除成功!!~

完整代码附上:

packagecn.wxz.mybatistest.test;importcn.wxz.mybatistest.pojo.User;importorg.apache.ibatis.io.Resources;importorg.apache.ibatis.session.SqlSession;importorg.apache.ibatis.session.SqlSessionFactory;importorg.apache.ibatis.session.SqlSessionFactoryBuilder;importorg.junit.Test;importjava.io.IOException;importjava.io.Reader;publicclassUserTest{@TestpublicvoidfindUserById(){String  resouces="mybatis-config.xml";Reader reader=null;try{
            reader=Resources.getResourceAsReader(resouces);}catch(IOException e){
            e.printStackTrace();}SqlSessionFactory sqlMapper=newSqlSessionFactoryBuilder().build(reader);SqlSession sqlSession=sqlMapper.openSession();User user=sqlSession.selectOne("findUserById",2);System.out.println(user.getUname());

        sqlSession.close();}@TestpublicvoidaddUserTest(){String  resouces="mybatis-config.xml";Reader reader=null;try{
            reader=Resources.getResourceAsReader(resouces);}catch(IOException e){
            e.printStackTrace();}SqlSessionFactory sqlMapper=newSqlSessionFactoryBuilder().build(reader);SqlSession sqlSession=sqlMapper.openSession();User user=newUser();
        user.setUname("王五");
        user.setUage(19);int ret = sqlSession.insert("addUser",user);
        sqlSession.commit();if(ret>0)System.out.println("添加成功!!");elseSystem.out.println("添加失败!!");
        

        sqlSession.close();}@TestpublicvoidupdateUserTest(){String  resouces="mybatis-config.xml";Reader reader=null;try{
            reader=Resources.getResourceAsReader(resouces);}catch(IOException e){
            e.printStackTrace();}SqlSessionFactory sqlMapper=newSqlSessionFactoryBuilder().build(reader);SqlSession sqlSession=sqlMapper.openSession();User user=newUser();
        user.setUid(1);
        user.setUname("小王同学");
        user.setUage(12);int ret=sqlSession.update("updateUser",user);if(ret>0)System.out.println("修改成功");elseSystem.out.println("修改失败");

        sqlSession.commit();
        sqlSession.close();}@TestpublicvoiddeleteUserTest(){String  resouces="mybatis-config.xml";Reader reader=null;try{
            reader=Resources.getResourceAsReader(resouces);}catch(IOException e){
            e.printStackTrace();}SqlSessionFactory sqlMapper=newSqlSessionFactoryBuilder().build(reader);SqlSession sqlSession=sqlMapper.openSession();int result=sqlSession.delete("deleteUser",1);if(result>0)System.out.println("删除成功");elseSystem.out.println("删除失败!");

        sqlSession.commit();
        sqlSession.close();}}

以上就是王同学给大家带来的mybatis连接mysql 实现CRUD!
觉得写的不错的话 三连奉上~
谢谢大家

在这里插入图片描述

标签: mybatis mysql java

本文转载自: https://blog.csdn.net/weixin_59796310/article/details/126780333
版权归原作者 学好c语言的小王同学 所有, 如有侵权,请联系我们删除。

“mybatis 连接mysql 实现CRUD”的评论:

还没有评论