创建数据库mybatis,表为user
CREATE DATABASE `mybatis`;
USE `mybatis`
CREATE TABLE `user`(
`id` INT(20) NOT NULL,
`name` VARCHAR(30) DEFAULT NULL,
`pwd` VARCHAR(30) DEFAULT NULL
)ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `user`(`id`,`name`,`pwd`)VALUES
(1,"张三","12345"),
(2,"李四","12345"),
(3,"王五","12345")
2.1.3 删除1,再在2右键新建module(模块,也就是一个子项目)
mysql
mysql-connector-java
5.1.44
org.mybatis
mybatis
3.5.5
junit
junit
4.13
test
2.2.2 Mybatis-Study下的pom,增加build
看清,别搞错pom.xml,此处是Mybatis-Study下的pom,注意放在哪
注意,此处也与狂神说笔记不同:
src/main/resources
**/*/.properties
**/*/.xml
false
src/main/java
**/*/.properties
**/*/.xml
false
包含:resources/mybatis-config.xml、resources/UserMapper、pojo.User、dao/UserDao、utils/MybatisUtils、UserDaoTest
注意这里的UserMapper.xml的放置,此处与狂神说笔记不一样
mybatis-config.xml:主要这里的
User
package com.cwq.pojo;
/**
* @author Chen
*/
// 实体类
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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", 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;
}
}
UserDao
package com.cwq.dao;
import com.cwq.pojo.User;
import java.util.List;
public interface UserDao {
// 查询用户信息
List getUser();
// 按id查询用户信息
User getUser1(int id);
// 增加用户信息
int addUser(User user);
// 修改用户信息
int updateUser(User user);
// 删除用户信息
int delUser(int id);
}
UserMapper.xml
MybatisUtils
package com.cwq.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;
/**
* @author Chen
*/
// sqlSessionFactory-->sqlSession
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
// mybatis 第一步:获取sqlSessionFactory对象
// 以下三步是固定的
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
// 既然有了sqlSessionFactory,顾名思义,我们就可以从中获得sqlSession实例
// sqlSession 完全包含了面向数据库执行SQL命令所需的所有方法
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
UserDaoTest:注意再哪建
package com.cwq.dao;
import com.cwq.pojo.User;
import com.cwq.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class UserDaoTest {
@Test
public void test(){
// 第一步:获得sqlsession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
// 执行sql:方式一,getMapper
UserDao userDao = sqlSession.getMapper(UserDao.class);
List userList = userDao.getUser();
userList.forEach(user -> System.out.println(user));
// 关闭sqlsession
sqlSession.close();
}
}