在上节IDEA+SpringBoot MyBatis Dynamic SQL的初体验(一)中,讲解了Mybatis Dynamic SQL数据库生成https://www.cnblogs.com/hjm0928/p/9955228.html
现在那看一下怎么使用
先看一下项目结构
可以看到实体类,Mapper文件和DynamicSqlSupport文件都生成成功了
第一步 修改application.properties修改为application.yml 内容如下
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/nice_test?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=UTC username: pigge password: 123456 http: encoding: force: true mybatis: mapper-locations: com.pigge.mybatistest.mapper server: tomcat: uri-encoding: UTF-8 port: 8080 logging: level: # 打开日志输出 com.pigge.mybatistest: DEBUG path: logs\spring debug: true
在创建UserService类
package com.pigge.mybatistest.service;
import com.pigge.mybatistest.entity.User;
import com.pigge.mybatistest.util.Page;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* @author hjm0928
*/
public interface UserService {
/**
* 根据用户名查询表内数据
* @param username 用户名
* @return 数据列表
*/
User selectByUsername(@Param("username")String username);
/**
* 分页查找数据
* @param page 分页工具类
* @return 记录集
*/
List<User> selectAll(Page page);
}
UserServiceImpl类
package com.pigge.mybatistest.service.impl; import com.pigge.mybatistest.entity.User; import com.pigge.mybatistest.mapper.UserDynamicSqlSupport; import com.pigge.mybatistest.mapper.UserMapper; import com.pigge.mybatistest.service.UserService; import com.pigge.mybatistest.util.Page; import com.sun.org.apache.bcel.internal.generic.InstructionComparator; import com.sun.org.apache.bcel.internal.generic.Select; import org.mybatis.dynamic.sql.render.RenderingStrategy; import org.mybatis.dynamic.sql.where.condition.IsEqualTo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; import static org.mybatis.dynamic.sql.select.SelectDSL.select; /** * @author hjm0928 */ @Service public class UserServiceImpl implements UserService { private final UserMapper userMapper; @Autowired public UserServiceImpl(UserMapper userMapper) { this.userMapper = userMapper; } @Override public User selectByUsername(String username) { InstructionComparator select = Select.getComparator(); //SQL语句为 select username, password, tel from user where username = ?; 参数username User user = userMapper.selectOne( select( UserDynamicSqlSupport.username, UserDynamicSqlSupport.password, UserDynamicSqlSupport.tel ).from(UserDynamicSqlSupport.user) .where(UserDynamicSqlSupport.username, IsEqualTo.of(()->username)) .build() .render(RenderingStrategy.MYBATIS3) ); //SQL语句 select username....(所有字段) from user where username = ?; 参数username user = userMapper.selectByExample() .where(UserDynamicSqlSupport.username, IsEqualTo.of(()->username)) .build() .execute() .get(0); return user; } @Override public List<User> selectAll(Page page) { //下一节实现 因为Mybatis Dynamic Sql没有默认的分页支持, 只能自定义 return null; } }
UserController类
package com.pigge.mybatistest.controller; import com.fasterxml.jackson.databind.util.JSONPObject; import com.pigge.mybatistest.entity.User; import com.pigge.mybatistest.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; /** * @author hjm0928 */ @Controller public class UserController { private final UserService userService; @Autowired public UserController(UserService userService) { this.userService = userService; } @RequestMapping("/get") @ResponseBody private User get(String username){ return userService.selectByUsername(username); } }
此时项目结构为
运行MyBatisTestApplication类, 在浏览器中输入http://localhost:8080/get?username=youUsername 其页面结果为
后台日志输出为
ok 测试成功