mybatis增删改查

Posted Hill_LAI

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis增删改查相关的知识,希望对你有一定的参考价值。

MybatisUtils类

package com.peak.util;

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 java.io.InputStream;

public class MybatisUtils {

    private static SqlSessionFactory sqlSessionFactory;

    static {
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static  SqlSession getSqlSession(){
         return sqlSessionFactory.openSession();
    }
}

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>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/peak_db?useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
                <property name="username" value="root"/>
                <property name="password" value="123123"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <mapper resource="com/peak/dao/UserMapper.xml"/>
    </mappers>

</configuration>

User类

package com.peak.pojo;

public class User {

    public User() {
    }

    public User(int id, String name, String pwd) {
        this.id = id;
        this.name = name;
        this.pwd = pwd;
    }

    private  int id;
    private String name;
    private String pwd;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\\'' +
                ", pwd='" + pwd + '\\'' +
                '}';
    }
}

UserMapper接口

package com.peak.dao;

import com.peak.pojo.User;
import java.util.List;

public interface UserMapper {
     List<User> getUserList();

     User getUserById(int id);

     int addUser(User user);

     int updateUser(User user);

     int deleteUser(int id);
}

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="com.peak.dao.UserMapper">
     <select id="getUserList" resultType="com.peak.pojo.User">
          select * from peak_db.user
     </select>

     <select id="getUserById" parameterType="int" resultType="com.peak.pojo.User">
          select * from peak_db.user where id = #{id}
     </select>

     <insert id="addUser" parameterType="com.peak.pojo.User">
          insert into peak_db.user (id, name, pwd) values (#{id},#{name},#{pwd})
     </insert>

     <update id="updateUser" parameterType="com.peak.pojo.User">
          update peak_db.user set name=#{name},pwd=#{pwd}  where id = #{id};
     </update>

     <delete id="deleteUser" parameterType="int">
          delete from peak_db.user where id = #{id};
     </delete>

</mapper>

UserDaoTest测试类

package com.peak.dao;

import com.peak.pojo.User;
import com.peak.util.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class UserDaoTest {

    @Test
    public void test(){

        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = mapper.getUserList();

        for(User user : userList){
            System.out.println(user);
        }

        sqlSession.close();
    }

    @Test
    public void getUserById(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        User userById = mapper.getUserById(1);

        System.out.println(userById);


        sqlSession.close();
    }

    @Test
    public void addUser()
    {
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        User user_1= new User(3,"xiaohua", "123123");

        mapper.addUser(user_1);

        System.out.println(user_1);

        //
        sqlSession.commit();

        sqlSession.close();
    }

    @Test
    public void updateUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        User user_1= new User(1,"huaband", "070569");

        mapper.updateUser(user_1);

        System.out.println(user_1);

        //
        sqlSession.commit();

        sqlSession.close();
    }

    @Test
    public void deleteUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        mapper.deleteUser(1);

        //
        sqlSession.commit();

        sqlSession.close();
    }
}

以上是关于mybatis增删改查的主要内容,如果未能解决你的问题,请参考以下文章

Mybatis实现增删改查

Mybatis增删改查

Maven+Mybatis实现数据库增删改查

mybatis之增删改查

[mybatis]快速搭建一个mybatis程序,实现对数据的增删改查

mybatis生成的增删改查怎么用