實戰mybatis-xml增刪改查

目錄結構java


在這裏插入圖片描述

package mybatis.dao;

import mybatis.entity.User;

import java.util.List;

public interface UserMapper {
      //查
      User selectUserById(int id);
      //增
      int addUser(User user);

      //刪
      int deleteUser(int id);
      //模糊查
      List<User> getUserByName(String string);
      //改
      int updataUser(User user);





}

package mybatis.entity;

public class User {

    private int id;
    private String username;
    private String password;

    public User(int id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }



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

    public int getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }



}

<?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/ssm?
useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf8"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>

<mappers>
    <mapper resource="mapper/Usermapper.xml"></mapper>
</mappers>


</configuration>

<?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="mybatis.dao.UserMapper">

    <select id="selectUserById" resultType="mybatis.entity.User" parameterType="int">

        select id,username,password from user where id = #{id}
    </select>

    <insert id="addUser" parameterType="mybatis.entity.User">
        insert into user (id,username,password) values (#{id},#{username},#{password})
    </insert>


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

    <select id="getUserByName" resultType="mybatis.entity.User" parameterType="string">

        select * from user where username like #{name}

    </select>
    <update id="updataUser" parameterType="mybatis.entity.User">
            update user set username=#{username},password=#{password} where id=#{id}
    </update>


</mapper>
import mybatis.dao.UserMapper;
import mybatis.entity.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.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class UserTest {
    private SqlSession session;

    @Before
    public void createSession(){
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new
                    SqlSessionFactoryBuilder().build(inputStream);
            session = sqlSessionFactory.openSession();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    @After
    public void closeSession(){
        session.commit();
        session.close();
    }

    @Test
    public void testFindUserById() {
            UserMapper mapper = session.getMapper(UserMapper.class);
            User user = mapper.selectUserById(1);
            System.out.println(user);

    }

    @Test
    public void testAddUser() {
        UserMapper mapper = session.getMapper(UserMapper.class);
        User user = new User(6,"張二","1111");
        int rows = mapper.addUser(user);
        System.out.println( rows);

    }
    @Test
    public void testDeleteUser() {
        UserMapper mapper = session.getMapper(UserMapper.class);
        int rows = mapper.deleteUser(5);
        System.out.println( rows);

    }

    @Test
    public void testgetUserByName() {
        UserMapper mapper = session.getMapper(UserMapper.class);
        String name = "%張%";
        List<User> users = mapper.getUserByName(name);
        for(User user:users){
            System.out.println(user.getUsername());
        }


    }

    @Test
    public void testUpdateUser() {
        UserMapper mapper = session.getMapper(UserMapper.class);
        User user = new User(5,"張四","1111");
       int rows = mapper.updataUser(user);

        System.out.println(rows);


    }




}

相關文章
相關標籤/搜索