一对一
路径:com/hcxy/mapper
PersonMapper.xml
<?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="com.hcxy.mapper.PersonMapper"> <!-- 一般为数据表在持久层对应接口的完整路径 -->
<select id="queryPersonById2" resultMap="twe" parameterType="Integer">
select *,p.id pid,c.id cid from tb_person p,tb_idcard c where p.card_id = c.id and p.id = #{id}
</select>
<!-- 自定义结果集-->
<resultMap id="twe" type="person">
<id column="pid" property="id"></id> <!-- 注意这里的id, column 的是数据库的id,property 是 实体类的id -->
<result column="name" property="name"></result>
<result column="sex" property="sex"></result>
<result column="age" property="age"></result>
<!-- 配置一对于映射结果集映射 -->
<association property="idCard" javaType="IdCard">
<id property="id" column="cid"></id> <!-- 注意这里的id, column 的是数据库的id,property 是 实体类的id -->
<result property="code" column="code"></result>
</association>
</resultMap>
</mapper>
实体
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {
private int id;
private String name;
private int age;
private String sex;
private IdCard idCard;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class IdCard {
private int id;
private String code;
}
还有service 层
一对多
xml
<?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">
<!-- 一对多映射,<collection>,ofType 重要属性之一 -->
<mapper namespace="com.hcxy.mapper.UserMapper"> <!-- 一般为数据表在持久层对应接口的完整路径 -->
<select id="UserMapperFind" resultMap="UserMapper" parameterType="Integer">
SELECT *,u.id uid,o.id oid from tb_user u,tb_orders o
where u.id = o.user_id and
u.id = #{id}
</select>
<resultMap id="UserMapper" type="User" >
<id property="id" column="uid"></id>
<result property="username" column="username"></result>
<result property="address" column="address"></result>
<collection property="orders" javaType="list" ofType="Orders">
<id property="id" column="oid"></id>
<result property="number" column="number"></result>
<result property="userId" column="user_id"></result>
</collection>
</resultMap>
</mapper>
实体:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Orders {
private int id;
private String number;
private int userId;
// private List<Person> personList;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private int id;
private String username;
private String address;
private List<Orders> orders;
}
多对多
xml名称要与接口名称一样,不然等着死
<?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="com.hcxy.mapper.OrderMapper"> <!-- 一般为数据表在持久层对应接口的完整路径 -->
<!-- 多对多得 查询 三个表 oid,pid 都是别名 映射 -->
<select id="OrderById" resultMap="OrderMapper" parameterType="Integer">
SELECT o.*,p.*, o.id oid, p.id pid from tb_orders o,tb_product p,tb_ordersitem io
where
o.id = io.orders_id
and
p.id = io.product_id
and
o.id = #{id}
</select>
<resultMap id="OrderMapper" type="Orders" >
<id property="id" column="oid"></id>
<result property="number" column="number"></result>
<collection property="personList" ofType="Product" javaType="list">
<id property="id" column="pid"></id>
<result property="name" column="name"></result>
<result property="price" column="price"></result>
</collection>
</resultMap>
</mapper>
实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Orders {
private int id;
private String number;
private int userId;
private List<Person> personList;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Product {
private int id;
private String name;
private double price;
private List<Orders> ordersList;
}
mabatis的写法
public class FactoryUtils {
public static SqlSessionFactory factory;
static {
String resource = "Employee-config.xml";
Reader reader = null;
try {
reader = Resources.getResourceAsReader(resource);
factory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getConnect(){
return factory.openSession();
}
}
配置文件
#取别名
mybatis.type-aliases-package=com.hcxy.pojo
# 数据库驱动:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 数据库连接地址
spring.datasource.url=jdbc:mysql://localhost:3306/books?useUnicode=true&characterEncoding=utf8&autoReconnect=true&serverTimezone=UTC&useSSL=false
# 数据库用户名&密码:
spring.datasource.username=root
spring.datasource.password=123456
<mapper namespace="com.hcxy.mapper.EmployeeMapper"> <!-- 一般为数据表在持久层对应接口的完整路径 -->
<resultMap id="userBean" type="Employee"> <!-- 一般为数据表对应实体类所在数据包 -->
<id property="id" column="uid"></id>
<result property="name" column="uname"></result>
<result property="age" column="cage"></result>
<result property="position" column="position"></result>
</resultMap>
<!-- 查询数据表中所有数据,这相标签的id值要与数据表对应接口中的查询方法的名称要一样 -->
<select id="findEmployee" resultMap="userBean" resultType="employee">
select * from employee
</select>
<insert id="saveEmployee" parameterType="employee">
insert into employee(uid,uname,cage,position)values (#{id},#{name},#{age},#{position})
</insert>
<update id="updateEmployee" parameterType="employee">
update employee set uname = #{name},cage = #{age},position = #{position} where uid = #{id}
</update>
<delete id="deleteEmployee" parameterType="Integer">
delete from employee where uid = #{id}
</delete>
</mapper>
AOP
package com.hcxy.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class MyAspect {
// @Pointcut("execution(* com.hcxy.service.*.*(..))")
@Pointcut("execution(* com.hcxy.service.ArticleServiceImpl.*(..))")
public void trans(){
}
@Before("trans()")
public void beforeAdvice(){
System.out.println("开始查询数据库");
}
@After("trans()")
public void after(JoinPoint point){
System.out.println("后置通知执行");
System.out.println(point.getClass());
System.out.println(point.getSignature().getName());
}
@Around("trans()")
public Object around(ProceedingJoinPoint point){
System.out.println("1111在方法执行前调用");
Object proceed = null;
try {
proceed = point.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
System.out.println("2222在方法执行后调用");
return proceed;
}
@AfterReturning("trans()")
public void after_return(JoinPoint point){
System.out.println("正常结束的后置通知执行");
}
@AfterThrowing("trans()")
public void after_throw(JoinPoint poin){
System.out.println("异常结束的后置通知执行");
}
}
一对多
@Data
public class User {
private int id;
private String username;
private String address;
private List<Orders> orders;
}
@Data
public class Orders {
private int id;
private String number;
private int userId;
// private List<Person> personList;
}
xml
<?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">
<!-- 一对多映射,<collection>,ofType 重要属性之一 -->
<mapper namespace="com.hcxy.dao.UserMapper"> <!-- 一般为数据表在持久层对应接口的完整路径 -->
<select id="getUserMapperByID" resultMap="UserMapper" parameterType="Integer">
SELECT *,u.id uid,o.id oid from tb_user u,tb_orders o
where u.id = o.user_id and
u.id = #{id}
</select>
<resultMap id="UserMapper" type="User" >
<id property="id" column="uid"></id>
<result property="username" column="username"></result>
<result property="address" column="address"></result>
<collection property="orders" javaType="list" ofType="Orders">
<id property="id" column="oid"></id>
<result property="number" column="number"></result>
<result property="userId" column="user_id"></result>
</collection>
</resultMap>
</mapper>
dao
public interface UserMapper {
List<User> getUserMapperByID(int id);
}
service
1.
public interface UserMapper {
List<User> getUserMapperByID(int id);
}
2.
import java.util.List;
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper userMapper;
@Override
public List<User> getUserMapperById(int id) {
return userMapper.getUserMapperByID(id);
}
}
Text
package com.hcxy.demo;
import java.util.List;
@SpringBootTest
public class ExamdemoApplicationTests {
@Autowired
private ArticleService articleService;
@Autowired
private PersonService personService;
@Autowired
UserService userService;
@Test
public void getMessage(){
Article result = articleService.getArticleById(1);
System.out.println(result);
}
@Test
public void getPersonById(){
Person person = personService.getPersonById(1);
System.out.println("person = " + person);
}
@Test
public void UserMapperById(){
List<User> list = userService.getUserMapperById(1);
System.out.println("list = " + list);
}
}