JDK|2.mybatis
大约 2 分钟
描述
原生方法
1. 引入mybatis依赖
<!-- pom.xml -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
2. 配置 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>
<!--别名-->
<typeAliases>
<!--命名空间-->
<package name = "com.gameserver.exorcistserver.pojo"/>
</typeAliases>
<!--设置连接数据库的环境-->
<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/ExorcistUser"/>
<property name="username" value="Admin"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!--引入映射文件-->
<mappers>
<package name = "com.gameserver.exorcistserver.mapper"/>
</mappers>
</configuration>
3. 创建mybatis映射文件-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.gameserver.exorcistserver.mapper.UserMapper">
<select id="selectAll" resultType="User">
SELECT * FROM sys_user
</select>
<select id="selectById" resultType="User">
SELECT * FROM sys_user WHERE id = #{id}
</select>
</mapper>
4.创建mybatis映射接口类-UserMapper.java
package com.gameserver.exorcistserver.mapper;
import com.gameserver.exorcistserver.pojo.User;
import java.util.List;
public interface UserMapper {
List<User> selectAll();
User selectById(int id);
}
5. 创建数据实体类-User.java
package com.gameserver.exorcistserver.pojo;
import lombok.Data;
import java.math.BigInteger;
@Data
public class User {
private BigInteger id;
private String name;
}
6.实例化
//1.加载mybati配置
SpringApplication.run(ExorcistServerApplication.class, args);
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取sqlSessionFactory,执行SQL
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//3.执行sql
List<User> users = userMapper.selectAll();
System.out.println(users);
//4.释放
sqlSession.close();
spring框架下的mybatis
1. 引入mybatis依赖
<!-- pom.xml -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
2. 配置application.yml代替mybatis-config.xml
server:
port: 8888
spring:
datasource:
url: jdbc:mysql://localhost:3306/ExorcistUser
username: Admin
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
# 类型别名包
type-aliases-package: com.gameserver.pojo
# xml映射
#mapper-locations: classpath:com.gameserver.mapper/*.xml
configuration:
map-underscore-to-camel-case: true
3.使用注解代替XMLMap映射
package com.gameserver.mapper;
import com.gameserver.exorcistserver.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface UserMapper {
@Select("SELECT * FROM sys_user")
List<User> selectAll();
@Select("SELECT * FROM sys_user WHERE id = #{id}")
User selectById(int id);
}
4. 自动扫描 Mapper 接口
package com.game.server;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.game.server.mapper")
public class ExorcistServerApplication {
public static void main(String[] args) {
SpringApplication.run(ExorcistServerApplication.class, args);
}
}
5. Autowired注解
package com.game.server.controller;
import com.game.server.mapper.UserMapper;
import com.game.server.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/user")
public class Controller {
@Autowired
private UserMapper userMapper;
@GetMapping("/getAllUserInfo")
public List<User> getAllUserInfo() {
return userMapper.selectAll();
}
}