Mybatis框架注解开发
@Insert:实现新增
@Update:实现更新
@Delete:实现删除
@Select:实现查询
@Result:实现结果集封装
@Results:可以与@Result 一起使用,封装多个结果集
@One:实现一对一结果集封装
@Many:实现一对多结果集封装
2 MyBatis的增删改查
2.1 使用xml实现curd
public interface UserMapper {
public void save(User user);
public void update(User user);
public void delete(int id);
public User findById(int id);
public List<User> findAll();
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="lifeilin.mapper.UserMapper">
<insert id="save" parameterType="lifeilin.domain.User">
insert into user
values (#{id}, #{username}, #{password}, #{birthday})
</insert>
<update id="update" parameterType="lifeilin.domain.User">
update user
set username = #{username},
password = #{password}
where id = #{id}
</update>
<delete id="delete" parameterType="int">
delete
from user
where id = #{id}
</delete>
<select id="findById" parameterType="int" resultType="lifeilin.domain.User">
select *
from user
where id = #{id}
</select>
<select id="findAll" resultType="lifeilin.domain.User">
select *
from user
</select>
</mapper>
import lifeilin.domain.User;
import lifeilin.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
public class MapperTest {
private UserMapper mapper;
@Before
public void before() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
mapper = sqlSession.getMapper(UserMapper.class);
}
@Test
public void testSave() {
User user = new User();
user.setId(11);
user.setUsername("观音");
user.setPassword("78999");
user.setBirthday(new Date());
mapper.save(user);
}
@Test
public void testUpdate() {
User user = new User();
user.setId(10);
user.setUsername("观音");
user.setPassword("78999");
user.setBirthday(new Date());
mapper.update(user);
}
@Test
public void testDelete() {
mapper.delete(11);
}
@Test
public void testFindById() {
User u = mapper.findById(9);
System.out.println(u);
}
@Test
public void testFindAll() {
List<User> all = mapper.findAll();
for (User user : all) {
System.out.println(user);
}
}
}
2.2 使用注解实现curd
删除映射关系文件(UserMapper.xml),在核心配置文件(sqlMapConfig.xml)种删除加载映射文件,添加加载映射关系。
<!-- <!– 加载映射文件–>-->
<!-- <mappers>-->
<!-- <mapper resource="lifeilin.mapper\UserMapper.xml"></mapper>-->
<!-- </mappers>-->
<!-- 加载映射关系 -->
<mappers>
<!-- 指定接口所在的包-->
<package name="lifeilin.mapper"/>
</mappers>
public interface UserMapper {
@Insert("insert into user values (#{id}, #{username}, #{password}, #{birthday})")
public void save(User user);
@Update("update user set username = #{username}, password = #{password} where id = #{id}")
public void update(User user);
@Delete("delete from user where id = #{id}")
public void delete(int id);
@Select("select * from user where id = #{id}")
public User findById(int id);
@Select("select * from user")
public List<User> findAll();
}
3 MyBatis的注解实现复杂映射开发
3.1 一对一的注解开发
一对一查询的需求:查询一个订单,与此同时查询出该订单所属的用户
在接口中使用注解配置:
第一种配置方法:
public interface OrderMapper {
@Select("SELECT *,`order`.id oid FROM `order` ,`user` WHERE `order`.uid = `user`.id;")
@Results({
@Result(column = "oid", property = "id"),
@Result(column = "ordertime", property = "ordertime"),
@Result(column = "total", property = "total"),
@Result(column = "uid", property = "user.id"),
@Result(column = "username", property = "user.username"),
@Result(column = "password", property = "user.password")
})
public List<Order> findAll();
}
第二种配置方法:
public interface OrderMapper {
@Select("SELECT * FROM `order`")
@Results({
@Result(column = "id", property = "id"),
@Result(column = "ordertime", property = "ordertime"),
@Result(column = "total", property = "total"),
@Result(
property = "user", //要封装的属性名称
column = "uid", //根据哪个字段去查询user表
javaType = User.class, //要封装的实体类型
//select属性 代表查询哪个接口的方法获得数据
one = @One(select = "lifeilin.mapper.UserMapper.findById")
)
})
public List<Order> findAll();
}
测试:
3.2 一对多的注解开发
一对多查询的需求:查询一个用户,与此同时查询出该用户具有的订单
@Select("select * from user")
@Results({
@Result(id = true, property = "id", column = "id"),
@Result(property = "username", column = "username"),
@Result(property = "password", column = "password"),
@Result(property = "birthday", column = "birthday"),
@Result(property = "orders", column = "id",
javaType = List.class,
many = @Many(select =
"lifeilin.mapper.OrderMapper.findByUid"))
})
List<User> findAllUserAndOrder();
3.3 多对多的注解开发
多对多查询的需求:查询用户同时查询出该用户的所有角色
@Select("select * from user")
@Results({
@Result(id = true, property = "id", column = "id"),
@Result(property = "username", column = "username"),
@Result(property = "password", column = "password"),
@Result(property = "roleList", column = "id",
javaType = List.class,
many = @Many(select =
"lifeilin.mapper.RoleMapper.findByUid")
)
})
List<User> findUserAndRole();