mybatis配置优化与增删改查

mybatis小结

mybatis配置优化

mybatis-config.xml配置

<?xml version="1.0" encoding="gbK" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--configuration核心配置文件-->
<configuration>
    <!--引入外部配置文件-->
    <properties resource="db.properties"/>
    <!--LOG4J日志工厂实现-->
    <settings>
        <setting name="logImpl" value="LOG4J"/>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
    <!--别名配置-->
    <typeAliases>
        <typeAlias type="com.wuxin.pojo.User" alias="User"/>
    </typeAliases>
    
    <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>
<!--配置mapper映射-->
    <mappers>
        <mapper resource="com/wuxin/dao/UserMapper.xml" />
    </mappers>
</configuration>

mybatis增删改查

接口文件

package com.wuxin.dao;
import com.wuxin.pojo.User;
import java.util.List;
import java.util.Map;

public interface UserMapper {
    //    需求1.根据用户ID查询用户表的记录
    List<User>getUser(int id);
    //    需求2.新增用户
    int addUser(Map map);
    //    需求3.根据ID删除用户
    int delUser(int id);
    //    需求4.随意更改某个用户数据
    int upperUser(Map map);
}

mapper.xml

<?xml version="1.0" encoding="gbK" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.wuxin.dao.UserMapper">
    <select id="getUser" parameterType="int" resultType="User">
        select * from mybatis.user
        where id = #{id};
    </select>
    <insert id="addUser">
        insert into mybatis.user (id, name, pwd)
        values (#{id},#{name},#{pwd});
    </insert>
    <delete id="delUser">
        delete mybatis.user from mybatis.user
        where id = #{id};
    </delete>
    <update id="upperUser" parameterType="User">
        update mybatis.user
        <trim prefix="set" suffixOverrides=",">
            <if test="name != null">name=#{name},</if>
            <if test="pwd != null">pwd=#{pwd},</if>
        </trim>
        where id = #{id}
    </update>
</mapper>

User实体类

package com.wuxin.pojo;

public class User {
    private int id;
    private String name;
    private String pwd;

    public User() {
    }

    public User(int id, String name, String pwd) {
        this.id = id;
        this.name = name;
        this.pwd = 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 + '\'' +
                '}';
    }
}

MybatisUtils工具类

package com.wuxin.utils;

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

public class MybatisUtils {

    private static SqlSessionFactory sqlSessionFactory;

    static {
        try {
            // 1.获取sqlSessionFactory对象
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    // 创建执行SQL的对象
    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession();
    }
}

测试类

package com.wuxin.dao;

import com.wuxin.pojo.User;
import com.wuxin.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserTest {
    @Test
    public void getUserList(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = mapper.getUser(1);
        System.out.println(userList);
        sqlSession.close();
    }

    @Test
    public void addUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        Map map = new HashMap();
        map.put("id",5);
        mapper.addUser(map);
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void delUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.delUser(5);
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void updateUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        Map map = new HashMap();
        map.put("id",1);
        map.put("name","无心");
        mapper.upperUser(map);
        sqlSession.commit();
        sqlSession.close();
    }
}
posted @ 2020-09-10 10:53  无心吖  阅读(175)  评论(0编辑  收藏  举报