关于配置文件
idea中mybatis可以自动将SQL中查出来的带下划线的字段,转换为驼峰标志
mybatis.configuration.map-underscore-to-camel-case=true
mybits查询返回集合
Sql查询需要返回多行数据,可以使用List集合来进行存储。首先需要在Mapper.xml文件中写上需要的sql语句,例如:
<select id="selectAllBusiness" resultMap="BaseResultMap" >
select * from tb_business
</select>
然后在Mapper文件中定义调用此sql语句的方法:
List<Business> selectAllBusiness();
在service的实现类中使用
List<Business> businessList;
businessList = businessMapper.selectAllBusiness();
就可以把查询出来的多行记录放入到List集合中了
Mybatis+mysql执行插入操作返回插入记录的Id
只需要在mapper文件的insert操作上加上useGeneratedKeys="true" keyProperty="userId"
keyProperty是user类的属性主键名称
<insert id="insertSelective" useGeneratedKeys="true" keyProperty="userId" parameterType="com.lanqiao.jd.entity.User" >
insert into tb_user (user_id, user_name, password,
balance, phone_number)
values (#{userId,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
#{balance,jdbcType=DECIMAL}, #{phoneNumber,jdbcType=VARCHAR})
</insert>
执行Insert操作后直接在service层使用
int userId = user.getUserId();
就可以获取到插入的记录的id了
Mybatis SpringBoot 注解 事务管理
首先在Application上添加@EnableTransactionManagement注解
@EnableTransactionManagement
@SpringBootApplication
public class JdApplication {
public static void main(String[] args) {
SpringApplication.run(JdApplication.class, args);
}
}
然后再需要进行事务管理的方法上添加 @Transactional
@Override
@Transactional
public Result register(User user) {}
注意:如果在方法中使用了try-catch操作,则注解不会生效
Mybatis多表连接查询
首先先写出正确的sql语句,因为多表连接通常较为复杂,所以需要在Mysql里面先测试。这次使用到了四个表(数据库设计有一点问题,血的教训)
查询语句如下,实现的模糊查询:
select a.product_id,product_name, product_price ,product_img_url ,business_name ,ifnull(comment_count,0)as comment_count from
(select p.product_id,p.product_name,p.product_price,p.product_img_url,b.business_name
from tb_product p NATURAL join tb_business b) as a
left join
(select p.product_id, count(*) as comment_count
from tb_comment c,tb_product p,tb_product_item pi
where c.product_item_id = pi.product_item_id and pi.product_id = p.product_id
GROUP BY p.product_id
) as b
on a.product_id = b.product_id
where product_name LIKE '%苹果%'
根据此查询语句的查询结果,新建一个实体类,我命名为ProdBusiComm(这是不好的命名习惯,别学我,没有见名知意),
实体类中的属性和查询结果的列一一对应:
public class ProdBusiComm {
private Integer productId;
private String productName;
private Integer productPrice;
private String productImgUrl;
private String businessName;
private Integer commentCount;
//省略getter and setter
}
注意数据库中的查询语句和属性名称的驼峰对应。
然后在任意一个Mapper中添加:
<!-- 主页->模糊查询 -->
<select id="fuzzyQueryProduct" resultType="com.lanqiao.jd.entity.ProdBusiComm" parameterType="java.lang.String" >
select a.product_id,product_name, product_price ,product_img_url ,business_name ,ifnull(comment_count,0)as comment_count from
(select p.product_id,p.product_name,p.product_price,p.product_img_url,b.business_name
from tb_product p NATURAL join tb_business b) as a
left join
(select p.product_id, count(*) as comment_count
from tb_comment c,tb_product p,tb_product_item pi
where c.product_item_id = pi.product_item_id and pi.product_id = p.product_id
GROUP BY p.product_id
) as b
on a.product_id = b.product_id
where product_name LIKE concat(concat("%",#{name,jdbcType=VARCHAR}),"%")
</select>
注意resutlType以及模糊查询在mapper.xml中描写方式。这样,查询结果就可以用ProdBusiComm来存放。