mybatis-plus版本为3.3.1.tmd
推荐idea的mybatisX插件,贼好用
介绍
https://mp.baomidou.com/guide/mybatisx-idea-plugin.html#%E5%8A%9F%E8%83%BD
代码如下
pom.xml
<mybatis-plus.version>3.3.1.tmp</mybatis-plus.version>
<!--mybatis-plus 引入 MyBatis-Plus 之后请不要再次引入
MyBatis 以及 MyBatis-Spring,以避免因版本差异导致的问题。-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
yml
#mybatis
mybatis-plus:
# 如果是放在src/main/java目录下 classpath:/com/yourpackage/*/mapper/*.xml
# 如果是放在resource目录 classpath:mapper/*.xml
mapper-locations: classpath:mapper/*.xml
#实体扫描,多个package用逗号或者分号分隔
typeAliasesPackage: com.hgf.user.entity
global-config:
db-config:
# AUTO(0)数据库ID自增 NONE(1)该类型为未设置主键类型(将跟随全局) INPUT(2)用户输入ID
id-type: auto
configuration:
map-underscore-to-camel-case: true
cache-enabled: false
call-setters-on-nulls: true
# 这个配置会将执行的sql打印出来,在开发或测试的时候可以用
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
dao
@Mapper
public interface UserDao extends BaseMapper<User> {
// 自定义sql
String getUserNameById(Integer id);
}
UserDao.xml
<select id="getUserNameById" resultType="java.lang.String">
select user_name from user where id = #{id}
</select>
Servcie
public interface UserService extends IService<User> {
String getUserNameById(Integer id);
User getUserById(Integer id);
}
ServiceImpl
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao,User> implements UserService {
@Override
public String getUserNameById(Integer id) {
return this.baseMapper.getUserNameById(id);
}
@Override
public User getUserById(Integer id) {
return this.getById(id);
}
}