Mybatis的缓存、druid、注解代替mapper.xml、嵌套代替对一、对多、分页

核心配置中默认有二级缓存
在mapper.xml中加入<cache></cache>即可 里面的属性默认即可
二级缓存需要关闭sqlSession或者事务提交
cache标签是namespace作用域下的即com.lee.mapper.IUserMapper下的,需要在类中序列化接口
注解代替mapper.xml,注解写在接口处,注解里面写sql语句
druid

druid

<!--pom配置文件加入配置-->
			<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.12</version>
            </dependency>
<dataSource type="com.lee.MyBatisDruid">
                <!--用于连接数据库-->
                <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://10.36.144.46:3306/test?useUnicode=true&amp;characterEncoding=utf-8&amp;serverTimezone=GMT%2B8&amp;useSSL=false"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>

                <!--使用配置文件的方式读取-->
                <!--<property name="driver" value="${driverClass}"/>-->
                <!--<property name="url" value="${url}"/>-->
                <!--<property name="username" value="${user}"/>-->
                <!--<property name="password" value="${password}"/>-->
</dataSource>
public class MyBatisDruid  extends PooledDataSourceFactory {

    public MyBatisDruid(){
       this.dataSource=new DruidDataSource();
    }

}

嵌套对一、对多

<!--resultMap解决属性和字段名称不一致情况 type就是返回值类型-->
    <!--<resultMap id="resultUserMap" type="manager">-->
        <!--<id column="id" property="id"></id>-->
        <!--<result column="name" property="mname"></result>-->
        <!--<result column="password" property="password"></result>-->
        <!--<result column="age" property="age"></result>-->
        <!--<result column="uid" property="uid"></result>-->
        <!--&lt;!&ndash;一对一、多对一 用association &ndash;&gt;-->
        <!--<association property="user" javaType="User">-->
            <!--<id column="uid" property="id"></id>-->
            <!--<result column="uname" property="name"></result>-->
            <!--<result column="upwd" property="password"></result>-->
            <!--<result column="uage" property="age"></result>-->

        <!--</association>-->
    <!--</resultMap>-->

    <!--<select id="selectManager" resultMap="resultUserMap">-->
<!--SELECT-->
	<!--m.*,u.id uid,u.name uname,u.password upwd,u.age uage-->
<!--FROM-->
	<!--manager m-->
	<!--INNER JOIN t_user u ON (-->
	<!--m.uid = u.id)-->
    <!--</select>-->
<resultMap id="resultUserMap" type="user">
        <id column="id" property="id"></id>
        <result column="name" property="name"></result>
        <result column="password" property="password"></result>
        <result column="age" property="age"></result>
        <result column="email" property="email"></result>
        <result column="salary" property="salary"></result>
        <result column="opendate" property="opendate"></result>
        <result column="png" property="png"></result>
        <result column="role" property="role"></result>
//select里面是另一个方法,语句,column的id是第一个查询语句查出来的id,作为方法的参数
        <!--多对多、一对多 collection -->
        <collection property="managerList"  select="com.lee.mapper.IUserMapper.selectManager1" column="id">
            <!--<id column="mid" property="id"></id>-->
            <result column="mname" property="mname"></result>
            <!--<result column="mpwd" property="password"></result>-->
            <!--<result column="mage" property="age"></result>-->
        </collection>
    </resultMap>
<select id="selectManager" resultMap="resultUserMap">
<!--起别名是因为两个表的字段名称重复了-->
SELECT
	u.*
FROM
	t_user u

    </select>
//这个方法selectManager1有参数#{uid},映射到上面的collection去调用
<select id="selectManager1" resultType="Manager">
        <!--起别名是因为两个表的字段名称重复了-->
        SELECT
        id id,name mname,age age,password password ,uid uid
        FROM
        manager
        where uid=#{uid}
    </select>

没有dao层的实现类的时候,使用mapper.xml作为实现类 service层返回一个mapper对象去调用dao层的相对的方法

public class UserServiceImpl extends BaseServiceImpl<Manager> implements IUserService {

    private IUserMapper mapper=null;
     {      SqlSession sqlSession=MybatisUtils.getSqlSession();
          mapper = sqlSession.getMapper(IUserMapper.class);
     }
    @Override
    protected IBaseDao<Manager> baseDao() {
        return mapper;
    }
}

分页需要加入配置

pom.xml中加入
<dependency>
		<groupId>com.github.pagehelper</groupId>
		<artifactId>pagehelper</artifactId>
		<version>5.1.10</version>
</dependency>

核心配置文件中

-<plugins>
<!-- 配置分页分页插件-->
加入分页插件
<plugin interceptor="com.github.pagehelper.PageInterceptor"/>
</plugins>

测试分页

@Test
    public void selectAll() {

        IUserService iUserService=new UserServiceImpl();
        //先设置显示的行数
        PageHelper.startPage(1,5);
        //调用方法
        ArrayList<Manager> managers = iUserService.selectAll();
        //managers实际是个page对象 通常使用PageInfo,所以再转
        Page page=(Page) managers;
        PageInfo pageInfo=new PageInfo(page);
        //page和pageInfo里面都有页数 条数 总页数总条数 数据
        List<Manager> list = pageInfo.getList();
        for (Manager manager : list) {
            System.out.println(manager);
        }
    }

maven项目中如果得不到传进来的参数,需要在pom.xml中配置告诉maven

<plugin>
      <artifactId>maven-compiler-plugin</artifactId>
     <version>3.8.0</version>
    <configuration>
        <compilerArgument>-parameters</compilerArgument>
   </configuration>
</plugin>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值