1.MyBatis的常用注解
注解开发越来越流行, Mybatis也可以使用注解开发方式,这样就可以减少编写Mapper映射文件。
@Insert:实现新增
@Update:实现更新
@Delete:实现删除
@Select:实现查询
@Result:实现结果集封装
@Results:可以与@Result 一起使用,封装多个结果集
@One:实现一对一结果集封装
@Many:实现一对多结果集封装
2.MyBatis的注解增删改查
mapper:
package com.cookie.mapper;
import com.cookie.pojo.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
public interface UserMapper {
List<User> findAll();
List<User> findAllUserAndRole();
//添加用户
@Insert("insert into user values(#{id},#{username},#{password},#{birthday})")
void addUser(User user);
//更新用户
@Update("update user set username = #{username} where id=#{id}")
void updateUser(User user);
//查询用户
@Select("select * from user")
List<User> selectUser();
//删除用户
@Delete("delete from user where id = #{id}")
void deleteUser(Integer id);
}
<mappers>
<!--扫描使用注解的类-->
<mapper class="com.cookie.mapper.UserMapper">
</mapper>
</mappers>
测试:
package com.cookie.test;
import com.cookie.mapper.OrderMapper;
import com.cookie.mapper.UserMapper;
import com.cookie.pojo.Order;
import com.cookie.pojo.User;
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 MybatisTest {
private UserMapper userMapper;
@Before
public void before() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
userMapper = sqlSession.getMapper(UserMapper.class);
}
@Test
public void addUser(){
User user = new User();
user.setId(3);
user.setUsername("12");
user.setPassword("34");
user.setBirthday(new Date());
userMapper.addUser(user);
}
@Test
public void selectUser(){
List<User> users = userMapper.selectUser();
for (User user : users) {
System.out.println(user);
}
}
@Test
public void deleteUser(){
userMapper.deleteUser(3);
}
@Test
public void updateUser(){
User user = new User();
user.setId(1);
user.setUsername("hahaha");
userMapper.updateUser(user);
}
}
3.MyBatis的注解实现复杂映射开发
实现复杂关系映射之前可以在映射文件中通过配置来实现,使用注解开发后,可以使用@Results注解,@Result注解, @One注解, @Many注解组合完成复杂关系的配置
4.一对一查询
package com.cookie.mapper;
import com.cookie.pojo.Order;
import com.cookie.pojo.User;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface OrderMapper {
@Select("select * from orders")
@Results({
@Result(id = true,property = "id",column = "id"),
@Result(property = "orderTime",column = "orderTime"),
@Result(property = "total",column = "total"),
@Result(property = "user",column = "uid",javaType = User.class,
one = @One(select = "com.cookie.mapper.UserMapper.findUserById"))
})
List<Order> findAllOrders();
}
package com.cookie.mapper;
import com.cookie.pojo.Order;
import com.cookie.pojo.User;
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface UserMapper {
//根据id查询用户信息
@Select("select * from user where id = #{id}")
User findUserById(Integer id);
}
@Test
public void findAllOrders(){
List<Order> allOrders = orderMapper.findAllOrders();
for (Order allOrder : allOrders) {
System.out.println(allOrder);
}
}
5.一对多查询
sql:
select * from user;
select * from orders where uid=查询出用户的id;
mapper:
public interface UserMapper {
@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 = "orderList",column = "id",javaType = List.class,
many = @Many(select = "com.cookie.mapper.OrderMapper.findOrderByUid"))
})
List<User> findAllUserAndOrder();
}
public interface OrderMapper {
@Select("select * from orders where uid = #{uid}")
List<Order> findOrderByUid(Integer uid);
}
@Test
public void findAllUserAndOrder(){
List<User> allUserAndOrder = userMapper.findAllUserAndOrder();
for (User user : allUserAndOrder) {
System.out.println(user);
}
}
6.多对多查询
sql:
select * from user;
select * from role r,user_role ur where r.id=ur.role_id and ur.user_id=用户的id
mapper:
public interface UserMapper {
@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 = "roleList",column = "id",javaType = List.class,
many = @Many(select = "com.cookie.mapper.RoleMapper.findById"))
})
List<User> findAllUserAndRoles();
}
package com.cookie.mapper;
import com.cookie.pojo.Role;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface RoleMapper {
@Select("select * from sys_role r,sys_user_role ur where r.id = ur.roleid and ur.userid=#{uid}")
List<Role> findById(Integer uid);
}
@Test
public void findAllUserAndRoles(){
List<User> allUserAndRoles = userMapper.findAllUserAndRoles();
for (User allUserAndRole : allUserAndRoles) {
System.out.println(allUserAndRole);
}
}