Mybatis接入SpringBoot项目
在pom.xml中编辑,先创建mysql-connector端的java连接
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>
采用阿里druid连接池管理
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
在application.properties配置文件中,导入mybatis需要的一些配置,用来启动一 个带有mybatis数据库访问的SpringBoot项目工程
mybatis.mapperLocation=classpath:mappong/*.xml
然后在resources下创建mapping文件夹
引入mybatis自动生成文件的插件
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<dependencies>
<!-- 对mysql解析 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>mybatis generator</id>
<phase>package</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<!-- 考虑到项目中反复生成文件的情况 -->
<configuration>
<!-- 允许移动生成的文件 -->
<verbose>true</verbose>
<!-- 允许自动覆盖文件 -->
<overwrite>true</overwrite>
<!-- mybatis generator配置文件的路径 -->
<configurationFile>
src/main/resources/mybatis-generator.xml
</configurationFile>
</configuration>
用户模块
使用 mybatis-generator 自动生成代码,只能根据主键查询,添加相应的方法
在UserPasswordDOMapper 添加:
UserPasswordDO selectByUserId(Integer userId);
在UserPasswordDoMapper.xml 添加
<select id="selectByUserId" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from user_password
where user_id = #{userId,jdbcType=INTEGER}
</select>
data object 是mybatis-generator 生成的与数据库相映射,视频里提出企业级开发中不能直接把dao 传输给前端,所以建立service层逻辑模型 model,同时UserModel 中封装了UserDOMapper,UserPasswordDOMapper,使用UserModel传递会返回一些不必要的信息(密码),所以在controller层定义了VO类传递。
UserModel
public class UserModel {
private Integer id;
private String name;
private Byte gender;
private Integer age;
private String telphone;
private String registerMode;
private String thirdPartyId;
private String encrptPassword;
...
}
service
public interface UserService {
UserModel getUserById(Integer id);
}
实现接口
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDOMapper userDOMapper;
@Autowired
private UserPasswordDOMapper userPasswordDOMapper;
@Override
public UserModel getUserById(Integer id){
UserDO userDO = userDOMapper.selectByPrimaryKey(id);
if(userDO == null){
return null;
}
UserPasswordDO userPasswordDO = userPasswordDOMapper.selectByUserId(userDO.getId());
return convertFromDataObject(userDO, userPasswordDO);
}
private UserModel convertFromDataObject(UserDO userDO, UserPasswordDO userPasswordDO) {
if (userDO == null) {
return null;
}
UserModel userModel = new UserModel();
BeanUtils.copyProperties(userDO, userModel);
if (userPasswordDO != null) {
userModel.setEncrptPassword(userPasswordDO.getEncrptPassword());
}
return userModel;
}
}
UserVO
public class UserVO {
private Integer id;
private String name;
private Byte gender;
private Integer age;
private String telphone;
...
}
UserController
@Controller("user")
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/get")
@ResponseBody
public UserVO getUser(@RequestParam(name="id") Integer id){
UserModel userModel = userService.getUserById(id);
return convertFromModel(userModel);
}
//userModel--->userVO
private UserVO convertFromModel(UserModel userModel){
if(userModel == null){
return null;
}
UserVO userVO = new UserVO();
BeanUtils.copyProperties(userModel, userVO);
return userVO;
}
}