MySQL小练习——做一个User表

做一个User表:
Id   username   password   roleId
能够实现对User表的持久化操作 进行 增 删 改 查操作 CURD
使用 mybatis Jdbc statement preparedStatement

写测试 测试这五个方法:
Public User query(String id);
Public List query(String username) 模糊查询
Public void update()
Public void insert()
Public void delete()

接口:

package com.self.mapper;

import com.self.pojo.User;

import java.util.List;

public interface UserMapper {

    public User query(String id);

    public User queryByName(String name);

    public List queryAll();

    public void updateById(User use);

    public void insert(User user);

    public void deleteById(String id);

}

User:

package com.self.pojo;


import org.apache.ibatis.type.Alias;

@Alias("user")
public class User {

    private String id;
    private String username;
    private String password;
    private String roleId;

    public String getId() {
        return id;
    }

    public void setId(String 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;
    }

    public String getRoleId() {
        return roleId;
    }

    public void setRoleId(String roleId) {
        this.roleId = roleId;
    }

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

测试:

package com.self.test;

import com.self.mapper.UserMapper;
import com.self.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 java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Test {
    public static void main(String[] args) throws IOException {

        String resource = "mybatis-config.xml";

        InputStream is = Resources.getResourceAsStream(resource);

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

        SqlSession session = sqlSessionFactory.openSession();

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

//        User user = new User();
//        user.setId("1");
//        user.setUsername("qiweifeng");
//        user.setPassword("123456");
//        user.setRoleId("01");
//
//        user.setId("2");
//        user.setUsername("hejinde");
//        user.setPassword("123456");
//        user.setRoleId("02");
//        mapper.insert(user);
//        session.commit();
//
//        User use1 = mapper.query("1");
//        System.out.println(use1);
//
//        User use2 = mapper.queryByName("qiweifeng");
//        System.out.println(use2);

//        List list =  mapper.queryAll();
//        System.out.println(list);

        User use = new User();
        use.setId("1");
        use.setUsername("qwf");
        use.setPassword("111111");
        use.setRoleId("3");
        mapper.updateById(use);
        session.commit();
//
//        mapper.deleteById("1");
//        session.commit();

    }

}

 

你可能感兴趣的:(mysql,mybatis,mysql,hibernate,lambda,leetcode)