Eclipse中实现SpringBoot与Mybatis整合(图⽂教程带源码)场景
数据库中数据
实现效果
项⽬结构
前⾯参照
Eclipse中新建SpringBoot项⽬并输出HelloWorld
Eclipse中新建SpringBoot项⽬完成对json、pojo、map、list的请求
实现
数据库设计
新建user表
业务实现
完成业务实现所需包的创建以及包下类的创建
pojo包下新建User.java
ample.demo.pojo;
public class User {
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
springboot架构图
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
resource下新建mapper⽂件夹,mapper下新建l
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-////DTD Mapper 3.0//EN"
"/dtd/mybatis-3-mapper.dtd">
<mapper namespace="ample.demo.mapper.UserMapper">
<select id="findAll" resultType="user">
select * from user
</select>
</mapper>
mapper下新建config⽂件夹,config下新建l
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-////DTD Config 3.0//EN"
"/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
新建service包,并在包下新建UserService.java接⼝类ample.demo.service;
import java.util.List;
import org.springframework.stereotype.Service;
ample.demo.pojo.User;
@Service
public interface UserService {
public List<User> findAll();
}
在service下新建UserServiceImpl.java
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;
ample.demo.mapper.UserMapper;
ample.demo.pojo.User;
ample.demo.service.UserService;
@Service
public class UserServiceImpl implements UserService {
//注⼊mapper接⼝代理对象
@Autowired
private UserMapper userMapper;
@Override
public List<User> findAll() {
// TODO Auto-generated method stub
List<User> list = userMapper.findAll();
return list;
}
}
新建mapper包并在下⾯新建UserMapper.java接⼝ample.demo.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
ample.demo.pojo.User;
@Mapper
public interface UserMapper {
/**
* 查询所有⽤户
*/
public List<User> findAll();
}
新建controller包并在下⾯新建UserController.java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
ample.demo.pojo.User;
ample.demo.service.UserService;
/**
* @author badao
* @Description:测试
* @Time:2019年3⽉6⽇下午11:20:19
*/
@RestController
public class UserController {
//注⼊Service服务对象
@Autowired
private UserService userService;
/**
* 整合SSM
*/
@RequestMapping("ssm")
public List<User> findAll(){
List<User> list = userService.findAll();
return list;
}
}
配置⽂件配置
打开application.properties
#springboot整合Mybatis框架
#加载Mybatis配置⽂件
mybatis.mapper-locations=classpath:mapper/*l
#定义别名
#数据源
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf8 spring.datasource.sql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123
打开l
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论