1、搭建实验的数据库
DROP TABLE IF EXISTS ` user ` ;
CREATE TABLE ` user ` (
` id` int NOT NULL AUTO_INCREMENT ,
` username` varchar ( 30 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
` pwd` varchar ( 100 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
PRIMARY KEY ( ` id` ) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
INSERT INTO ` user ` VALUES ( 1 , 'madongmei' , '123456' ) ;
INSERT INTO ` user ` VALUES ( 2 , 'xialuo' , '123456' ) ;
INSERT INTO ` user ` VALUES ( 3 , 'dachun' , '123456' ) ;
SET FOREIGN_KEY_CHECKS = 1 ;
2、在maven项目中导入mybatis和mysql所需要的依赖
< dependency>
< groupId> org.mybatis</ groupId>
< artifactId> mybatis</ artifactId>
< version> 3.5.7</ version>
</ dependency>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
< version> 8.0.28</ version>
</ dependency>
3、编写mybatis核心配置文件(mybatis-config.xml)
<?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.cj.jdbc.Driver" />
< property name = " url" value = " jdbc:mysql://localhost:3306/mybatis?characterEncoding=UTF-8& useSSL=true& useUnicode=true" />
< property name = " username" value = " root" />
< property name = " password" value = " 123456" />
</ dataSource>
</ environment>
</ environments>
</ configuration>
4、编写mybatis工具类(MybatisUtil.java)
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 {
String resource = "mybatis-config.xml" ;
InputStream inputStream = Resources. getResourceAsStream ( resource) ;
sqlSessionFactory = new SqlSessionFactoryBuilder ( ) . build ( inputStream) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
public static SqlSession getSession ( ) {
return sqlSessionFactory. openSession ( ) ;
}
}
5、编写实体类
public class User {
private int id;
private String username;
private String pwd;
public User ( ) {
}
public User ( int id, String username, String pwd) {
this . id = id;
this . username = username;
this . pwd = pwd;
}
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 getPwd ( ) {
return pwd;
}
public void setPwd ( String pwd) {
this . pwd = pwd;
}
@Override
public String toString ( ) {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", pwd='" + pwd + '\'' +
'}' ;
}
}
6、编写Mapper/Dao接口类(UserMapper)
import com. liu. pojo. User;
import java. util. List;
public interface UserMapper {
List< User> selectUser ( ) ;
}
7、编写UserMapper.xml配置文件
<?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 = " com.liu.dao.UserMapper" >
< select id = " selectUser" resultType = " com.liu.pojo.User" >
select * from user
</ select>
</ mapper>
8、编写测试类(导入Junit依赖)
public class UserDaoTest {
@Test
public void Test ( ) {
SqlSession session = MybatisUtils. getSession ( ) ;
UserMapper mapper = session. getMapper ( UserMapper. class ) ;
List< User> users = mapper. selectUser ( ) ;
for ( User user: users) {
System. out. println ( user) ;
}
session. close ( ) ;
}
}
出现的问题:
1、Maven静态资源过滤问题(注意不要把true写成false)
< build>
< resources>
< resource>
< directory> src/main/java</ directory>
< includes>
< include> **/*.properties</ include>
< include> **/*.xml</ include>
</ includes>
< filtering> true</ filtering>
</ resource>
< resource>
< directory> src/main/resources</ directory>
< includes>
< include> **/*.properties</ include>
< include> **/*.xml</ include>
</ includes>
< filtering> true</ filtering>
</ resource>
</ resources>
</ build>
2、mybatis配置文件中一定要加上这条,注意不要把"/“写成”."
< mappers>
< mapper resource = " com/liu/dao/UserMapper.xml" />
</ mappers>