创建SpringBoot项目
引入相关依赖
<!-- web核心依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<!-- 自动创建管理表依赖actable, 如果想手动管理可以不要这个依赖 -->
<dependency>
<groupId>com.gitee.sunchenbin.mybatis.actable</groupId>
<artifactId>mybatis-enhance-actable</artifactId>
<version>1.5.0.RELEASE</version>
</dependency>
<!-- 可以根据自己的需求配置数据库, 如果嫌麻烦直接使用H2数据库 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.1.212</version>
<scope>runtime</scope>
</dependency>
创建相应的目录结构
创建Entity
这里的@Table, @Id, @Column用actable或者java自己的都可以
@Data
@Builder
@Table(name = "tb_user")
public class User implements Serializable{
/**
*
*/
private static final long serialVersionUID = -8720709928795156685L;
@Id
private long id;
@Column
private String username;
@Column
private String password;
}
创建Mapper接口
@Mapper
public interface UserMapper {
List<User> findAll();
void save(User user);
}
Mapper文件映射
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.example.demo.mapper.UserMapper">
<!-- findAll返回的是列表,需要手动映射 -->
<resultMap type="com.example.demo.model.User" id="userMap">
<id column="id" property="id" jdbcType="INTEGER" />
<result column="username" property="username" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
</resultMap>
<select id="findAll" resultType="com.example.demo.model.User" resultMap="userMap">
SELECT * FROM tb_user
</select>
<insert id="save" parameterType="com.example.demo.model.User">
insert into tb_user values(#{id}, #{username}, #{password})
</insert>
</mapper>
创建接口
UserService.java
public interface UserService {
List<User> findAll();
void save(User user);
}
创建实现类
UserServiceImpl.java
@Service
public class UserServiceImpl implements UserService{
@Autowired
UserMapper userMapper;
@Override
public List<User> findAll() {
// 为了方便先insert一条数据
User user = User.builder()
.id(111)
.username("helloworld")
.password("still helloworld")
.build();
save(user);
return userMapper.findAll();
}
@Override
public void save(User user) {
userMapper.save(user);
}
}
创建Controller
UserController.java
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping
public List<User> findAll(){
return userService.findAll();
}
}
主程序入口类配置
MybatisDemoApplication.java
/*
* com.gitee.sunchenbin.mybatis.actable.dao.* 和
* com.gitee.sunchenbin.mybatis.actable.manager.* 是actable的官方配置,照抄就行
*/
@SpringBootApplication
@MapperScan({ "com.gitee.sunchenbin.mybatis.actable.dao.*", "com.example.demo.mapper"})
@ComponentScan(basePackages = { "com.gitee.sunchenbin.mybatis.actable.manager.*", "com.example.demo.*"})
public class MybatisDemoApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisDemoApplication.class, args);
}
}
配置文件
server:
port: 8888
spring:
datasource:
# --- begin mysql config ---
# url: jdbc:mysql://localhost:3306/mybatis_demo?characterEncoding=UTF-8
# driver-class-name: com.mysql.cj.jdbc.Driver
# username: your_database_username
# password: your_database_password
# --- end mysql config ---
url: jdbc:h2:mem:testdb;mode=MySQL
driver-class-name: org.h2.Driver
mybatis:
mapper-locations:
- classpath:mapper/*.xml # 自己的mapper配置目录
# actable的官方配置,照抄就ok
- classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/*/*.xml
table:
# create: 会删除原有所有数据在新建; none:不作任何处理; update: 不存在新建,存在更新
auto: update
model:
pack: com.example.demo.model # entity目录
database:
type: mysql
启动并测试
如果之前的配置用的是H2数据库的话,直接启动后访问http://localhost:8888/users会返回如下数据,如果用的是mysql数据库的配置,请确定指定的数据库已安装并且已启动。
感谢阅读