1.引入坐标
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.1.2</version>
</dependency>
2.sqlMapConfig.xml中配置插件类
<plugins>
<plugin interceptor="tk.mybatis.mapper.mapperhelper.MapperInterceptor">
<!--通用Mapper接口,多个通用接口用逗号隔开-->
<property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
</plugin>
</plugins>
3.使用注解配置实体类
@Table(name = "t_user")//表名
public class User {
@Id//主键
@GeneratedValue(strategy = GenerationType.IDENTITY)//主键生成策略
private Integer id;
private String username;
}
4.定义通用mapper接口
public interface UserMapper extends Mapper<User> {
}
5.使用
//select
User user = userMapper.selectOne(user); //只返回一条
List<User> users = userMapper.select(null); //查询全部
//insert
int insert = userMapper.insert(user); //null值会保存
int i = userMapper.insertSelective(user); //null值不会保存
网友评论