创建SpringBoot项目
引入相关依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.gitee.sunchenbin.mybatis.actable</groupId>
<artifactId>mybatis-enhance-actable</artifactId>
<version>1.5.0.RELEASE</version>
</dependency>
<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() {
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
@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:
url: jdbc:h2:mem:testdb;mode=MySQL
driver-class-name: org.h2.Driver
mybatis:
mapper-locations:
- classpath:mapper/*.xml
- classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/*/*.xml
table:
auto: update
model:
pack: com.example.demo.model
database:
type: mysql
启动并测试
如果之前的配置用的是H2数据库的话,直接启动后访问http://localhost:8888/users会返回如下数据,如果用的是mysql数据库的配置,请确定指定的数据库已安装并且已启动。
github源码
感谢阅读
|