Maven依赖管理

本文详细介绍了如何在Maven项目中设置JDK版本,管理依赖的优先级、范围和排除,以及利用mapper代理进行数据库操作和优化。还涵盖了Maven的二级缓存配置和面试题知识点。
摘要由CSDN通过智能技术生成
pom相关配置:

指定jdk版本(运行警告):

1.在pom.xml设置<proerties>设置编码,源码编译jdk版本,target编译时使用的jdk版本

2.指定编译插件的jdk版本

3.修改maven的settings.xml文件(不利于之后应用其他版本的修改)

依赖的优先级

1.直接依赖的优先级高于间接依赖

2.都是直接依赖或者间接依赖,下面依赖的优先级高于上面的

3. A-依赖->B-依赖->C-依赖->D A-依赖->E-依赖->D    间接依赖的路径越短优先级越高

依赖统一管理,可以在<proerties>统一依赖版本

依赖的排除

当想更改间接依赖的某个依赖的版本不受直接依赖的影响可以在<dependency>使用<exclusion>进行排除

maven常见的scope有(面试题):<compile><provided><runtime><test><system>

如果依赖的scope为test,这个依赖不会进行传递

依赖范围对于编译classpath有效对于测试classpath有效对于运行classpath有效例子
compilespring -core
testjunit、spring-test
providedservlet-api、jsp-api
runtimeJDBC驱动(一般直接使用compile)
system本地的maven仓库之外的类库
继承与聚合

在父类<dependencyManagement>中写入引用依赖,子类继承父类需要使用的依赖在pom.xml写出:

父类需要配置包含的子类

 <modules>
        <module>user-common</module>
        <module>user-dao</module>
        <module>user-service</module>
        <module>user-web</module>
    </modules>

继承类

<parent>
    <groupId>com.fs</groupId>
    <artifactId>user-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>
mapper代理

maven的mapper代理优化sql数据库的连接,不需要我们建立dao层的接口与实现类,而是通过SqlSession对象完成数据库的操作。

在使用mapper时需要配置mapper.xml文件,输入sql语句输入详细对象类与实现类

<select id="selectByStNo" parameterType="int" resultType="com.fs.entity.Student">
        select * from tb_student where id = #{id}
 </select>
 <select id="selectAll" resultType="com.fs.entity.Student">
        select * from tb_student
 </select>
    <!--增删改-->
 <insert id="addStudent" parameterType="Student">
        insert into tb_student (StNo,name,sex,age) values (#{StNo},#{name},#{sex},#{age})
  </insert>
  <delete id="delStudent" parameterType="student">
        delete from tb_student where StNo =#{StNo}
  </delete>
  <update id="updateStu" parameterType="Student">
        update tb_student set name = #{name} , sex = #{sex} , age = #{age} where StuNo=#{StuNo}
  </update>

(实现学生对象的增删改查等功能)

代理规范:

接口名称需要与映射配置文件名称相同

映射配置文件中namespace必须是接口的全名(所在的包名与方法名)

<mapper namespace="com.fs.mapper.UserMapper">
    <!--配置文件是在com.fs.mapper包下的UserMapper接口文件-->
</mapper>

接口中的方法名和映射配置文件中的标签的id一致(接口引用方法名是配置文件中的方法名)

public interface UserMapper {
    public User queryById(int id) throws SQLException;
    public List<User> queryAll() throws SQLException;
    public boolean add(User user) throws SQLException;
    public boolean update(User user) throws SQLException;
    public boolean delete(int id) throws SQLException;

    List<User> list(int startNo, int pageSize) throws SQLException;

    long count() throws SQLException;
}
<mapper namespace="com.fs.mapper.UserMapper">

<select id="queryById" parameterType="User" resultType="com.fs.entity.User">
    SELECT * FROM tb_userinfo WHERE id=#{id}
</select>
    <select id="queryAll" resultType="com.fs.entity.User">
        SELECT * FROM tb_userinfo
    </select>
    <insert id="add" parameterType="User">
        INSERT INTO tb_userinfo VALUES (#{id},#{name},#{sex},#{age},#{address},#{email},#{qq})
    </insert>
    <update id="update" parameterType="User">
        UPDATE tb_userinfo SET 'name'=#{name},'sex'=#{sex},'age'=#{age},'address'=#{address},'email'=#{email},'qq'=#{qq}" +
        "WHERE 'id'=#{id}
    </update>
    <delete id="delete" parameterType="User">
        DELETE FROM `tb_userinfo` WHERE `id` = #{id}
    </delete>
<select id="list" resultType="com.fs.entity.Page">
    SELECT * FROM tb_userinfo LIMIT #{startNo},#{pageSize}
</select>
    <select id="count" resultType="long">
        SELECT COUNT(1) FROM tb_userinfo
    </select>

</mapper>

接口中的返回值类型和映射配置文件中的resultType指定的类型一致

mapper属性封装

在创建的一个属性中,包含另一个已经创建的属性类,实现代码不冗余

public class Student{
	private String name;
	private int id;
	private String sex;
	private int age;
}
public class Studentgrade{
    private double score;
    private String course;
    private Student student;
}//Studentgrade中包含Student类,Studentgrade中也有Student类中的所有属性值
mapper动态搜索
Mybatis提供支持动态sql的实现:

动态sql标签:

1、if:if条件

<if test="条件(属性名)" >
	sql片段
</if>

2、where:where关键字(能去掉第一个'and' 'or'连接词,只能去除字段前的 )

3、foreach:循环标签

<foreach collection="数据类型" item="变量值" separator="sql片段的连接符" open="sql片段的前部分"></foreach>

4、set:set关键字(可以去掉最后一个逗号)

5、sql:sql片段,重复sql的重用

mapper的二级缓存

基于Mapper的二级缓存,不同的sqlSession执行相同的mapper(namespace)中相同sql语句,并且传递参数,也就是执行相同的sql语句,就会先从二级缓存查询数据,如果二级缓存没有数据,才到数据库查询,如果二级缓存有数据,返回缓存中的数据

mybatis的全局二级缓存开启(表示mybatis允许使用二级缓存)

<settings>
    <setting name="cacheEnabled" value="true"/>
</settings>
当二级缓存没有开启

a、开启前使用<cache/>标签:表示该Mapper下的所有查询都进行二级缓存<cache/>有type属性,指定cache的实现类,默认值:SerializedCache(序列化的缓存实现)

面试题:
	自定义mybatis二级缓存,这么实现?
		1、编写一个类实现Cache接口
		2、在Mapper配置文件中,<cache type="Cache实现类的全限定名"/></span></span>

b、对mapper下查询的返回值类型,实现可序列化接口:SerializedCache要求数据进行序列化操作

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值