JavaWeb(p116-p134)

#驱动类名称
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#数据库连接的ur1
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis
#连接数据库的用户名
spring.datasource.username=root
#连接数据库的密码
spring.datasource.password=**********

2.1 数据库连接池

数据库连接池是个容器,负责分配,管理数据库连接,默认是hikari,可以通过依赖换成Druid

 2.1.4 lombok技术

引入以后自动生成构造器等一系列方法,要引入到pom中

@Data = getter+setter+tostring+hashcode

@AllArgsConstructor:提供有参构造

@NoArgsConstructor:提供无参构造

2.2 Mybatis基础操作

2.2.1名字不一致

通过Mapper(Dao)里进行sql操作 @Select("sql.....") 等

实体类属性名和数据库表的名字如果不一致,可以有以下几种方法

1. 给数据库中的名字起别名

@Select("select id, username, password, name, gender, image, job, entrydate, " +
        "dept_id AS deptId, create_time AS createTime, update_time AS updateTime " +
        "from emp " +
        "where id=#{id}")
public Emp getById(Integer id);

 2. 手动结果映射,在Mapper中这样做:

@Results({@Result(column = "dept_id", property = "deptId"),
          @Result(column = "create_time", property = "createTime"),
          @Result(column = "update_time", property = "updateTime")})
@Select("select id, username, password, name, gender, image, job, entrydate, dept_id, create_time, update_time from emp where id=#{id}")
public Emp getById(Integer id);

 3. 最为推荐:在配置文件中引入驼峰命名转换,前提是要严格对应

# 在application.properties中添加:
mybatis.configuration.map-underscore-to-camel-case=true

2.2.2 #{name}

精确匹配中,要求#{name}外面不能加入‘’,否则会无效,会采用mysql提供的字符串拼接函数:

concat(‘%’,#{name},‘%’),这样就解决了SQL注入的风险

2.2.3 主键返回

数据添加成功以后,需要获取插入数据库数据的主键,要在Mapper接口中的方法上加入一个注解:@Option(useGeneratedKeys = true,keyProperty = “id”)

2.2.4 日志输出

#指定mybatis输出日志的位置, 输出控制台
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

3.1XML文件配置规范

有的时候增删改查过程比较复杂,所以最好拿出来单写

1.xml映射文件的名称要与接口名称保持一致,并且要和mapper接口放在相同包下(同包同名)

2.xml的namespace属性与mapper接口的全名一致

3.xml中的sql语句的id与mapper接口的方法名一致,返回类型一致

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="">
 
</mapper>

4.1 动态SQL

if:如果为true则拼接sql

where:只会在子元素有内容的情况下才插入where语句,而且会自动去除子句开头的and或者or

set:在update语句中可以删除多余的逗号
foreach:支持批量删除

<foreach collection="集合名称" 
		 item="集合遍历出来的元素/项" 
		 separator="每一次遍历使用的分隔符" 
         open="遍历开始前拼接的片段" 
         close="遍历结束后拼接的片段">
</foreach>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.mapper.EmpMapper">
    <!--删除操作-->
    <delete id="deleteByIds">
        delete from emp where id in
        <foreach collection="ids" item="id" separator="," open="(" close=")">
            #{id}
        </foreach>
    </delete>
</mapper> 

sql&include

 sql:定义可重用的sql片段

<sql id="commonSelect">
 	select id, username, password, name, gender, image, job, entrydate, dept_id, create_time, update_time from emp
</sql>

include:在原来抽取的地方进行引用

<select id="list" resultType="com.itheima.pojo.Emp">
    <include refid="commonSelect"/>
    <where>
        <if test="name != null">
            name like concat('%',#{name},'%')
        </if>
        <if test="gender != null">
            and gender = #{gender}
        </if>
        <if test="begin != null and end != null">
            and entrydate between #{begin} and #{end}
        </if>
    </where>
    order by update_time desc
</select>

  • 15
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值