自己理解的是:
思路:搭建环境–>导入Mybatis–>编写代码–>测试!
思路:搭建环境–>导入Mybatis–>编写代码–>测试!
CREATE DATABASE mybatis ;
USE mybatis;
CREATE TABLE `user`(
`id` INT(20) NOT NULL PRIMARY KEY,
`name` VARCHAR(32) DEFAULT NULL,
`pwd` VARCHAR(32) DEFAULT NULL
)ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `user` (`id`,`name`,`pwd`) VALUES
(1,'张三','123456'),
(2,'李四','123456'),
(3,'王五','123456')
mysql
mysql-connector-java
5.1.47
org.mybatis
mybatis
3.5.2
junit
junit
4.12
//找接口的映射文件
src/main/resources
**/*.properties
**/*.xml
src/main/java
**/*.properties
**/*.xml
package org.westos.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;
/**
* @Description:TODO
* @Author:@李小白
* @Date:2019/9/28 21:33
*/
public class MyBatis {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
package com.kuang.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 + '\'' +
'}';
}
}
package org.westos.dao;
import org.westos.pojo.User;
import java.util.List;
/**
* @Description:TODO
* @Author:@李小白
* @Date:2019/9/28 20:59
*/
public interface UserMapper {
//查询所有的数据
List getFindAll();
//用id进行查询数据
User getFindUserById(int id);
//进行模糊查询,根据姓名进行模糊查询
List getUserLike(String name);
//添加一个用户
int getAddUser(User user);
//根据id更改一个用户
int getUpdateUser(User user);
//根据id进行删除一个用户
int getDeleteUser(int id);
}
insert into mybatis.user (id,name ,pwd) values (#{id},#{name},#{pwd});
update mybatis.user set name=#{name},pwd=#{pwd} where id=#{id};
delete from mybatis.user where id=#{id};
package org.westos.dao;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.westos.pojo.User;
import org.westos.utils.MyBatis;
import java.util.List;
/**
* @Description:这是一个方便进行测试的类
* @Author:@李小白
* @Date:2019/9/28 21:38
*/
public class MyTest {
//开始进行查询所有的用户
UserMapper mapper;
SqlSession sqlSession;
@Before
public void getBefore(){
sqlSession = MyBatis.getSqlSession();
mapper = sqlSession.getMapper(UserMapper.class);
}
@Test
public void getFangAll(){
List findAll = mapper.getFindAll();
for (User user : findAll) {
System.out.println(user);
}
sqlSession.close();
}
//根据id进行查询用户
@Test
public void getFindUserById(){
User findUserById = mapper.getFindUserById(1);
System.out.println(findUserById);
sqlSession.close();
}
//添加一个用户,一定要记住一个问题,增删改一定要进行手动提交
@Test
public void getAddUser(){
int name = mapper.getAddUser(new User(5, "赵六", "12333"));
sqlSession.commit();
System.out.println(name);
}
//更改一个客也是有问题,记得手动提交
@Test
public void getUpdateUser(){
int update = mapper.getUpdateUser(new User(2, "小李", "53453"));
sqlSession.commit();
System.out.println(update);
}
//对数据库好像不管用
@Test
public void getDeleteUser(){
int deleteUser = mapper.getDeleteUser(1);
sqlSession.commit();
System.out.println(deleteUser);
}
//对数据进行模糊查询,这个的问题就是应该返回的第一个List
@Test
public void getUserLike(){
List userLike = mapper.getUserLike("%小%");
for (User user : userLike) {
System.out.println(user);
}
}
@After
public void getAfter(){
sqlSession.close();
}
}