mybatis框架-01
程序员文章站
2024-03-14 20:12:17
...
mybatis
- 在idea中新建一个maven空项目名为Mybatis
- 在这个空项目下新建一个module名为mybatis-01,以及新建以下的文件
在Mybatis的pom.xml中导入依赖文件以便所有的module都可以使用
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hu</groupId>
<artifactId>Mybatis</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>mbatis-01</module>
</modules>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
- 新建一个User类
package com.hu.pojo;
import lombok.*;
@AllArgsConstructor
@NoArgsConstructor
@Setter
@Getter
@ToString
public class User {
private Integer id;
private String username;
private String password;
}
- 新建一个UserMapper接口(用于操作数据库)
package com.hu.mapper;
import com.hu.pojo.User;
import java.util.List;
public interface UserMapper {
List<User> getUserList();
}
- 在该类的同包下新建一个UserMapper.xml用于对User类进行CRUD的配置
<?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.hu.mapper.UserMapper">
<select id="getUserList" resultType="com.hu.pojo.User">
select * from mybatis.user
</select>
</mapper>
- 新建一个MybatisUtils类作为工具类获取SqlSession对象
package com.hu.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;
public class MybatisUtils {
private static InputStream resourceAsStream = null;
private static SqlSessionFactory build = null;
static {
try {
resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
build = new SqlSessionFactoryBuilder().build(resourceAsStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession() {
return build.openSession();
}
}
- 在test包下新建一个test类进行测试
package com.hu.mapper;
import com.hu.pojo.User;
import com.hu.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class UserMapperTest {
@Test
public void test1(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> list = mapper.getUserList();
for (User user : list) {
System.out.println(user);
}
}
}
上一篇: SSM实现分页查询Demo
下一篇: 牛客网-华为机试刷题(二)