Mybatis

Mybatis中部分配置文件:

还有部分设置配置和环境配置

//配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/db4"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/wlh/dao/UserMapper.xml"/>
    </mappers>
</configuration>
//创建SqlSesson对象
public class MybatisUtils {
	static SqlSessionFactory sqlSessionFactory;

	static{
		try {
			String resource = "mybatis-config.xml";
			//这个是mybatis配置文件
			InputStream inputStream = Resources.getResourceAsStream(resource);
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static SqlSession getsqlsSession(){
		return sqlSessionFactory.openSession();
	}
}
//测试阶段
public void test() {
		SqlSession sqlSession = MybatisUtils.getsqlsSession();
		UserMapper mapper = sqlSession.getMapper(UserMapper.class);
		//获取UserMapper实现类的对象
		List<User> userList = mapper.getUserList();
		for (User user : userList) {
			System.out.println(user);
		}
		sqlSession.close();
	}

注解的使用

多表查询

联合查询
1 -> 1: student中有个属性是Teacher teacher这种情况使用:
< association property=“teacher” javaType=“Teacher”>

 <select id="getStudent" resultMap="getStudents">
 //使用的隐式内连接查询
        select
            s.*,t.id tid,t.name tname
        from
            student s,teacher t
        where
            s.tid = t.id
        order by id asc
    </select>
    <resultMap id="getStudents" type="student">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <association property="teacher" javaType="teacher">
            <id property="id" column="tid"/>
            <result property="name" column="tname"/>
        </association>
    </resultMap>

1 -> n : teacher里面有个属性是list< student> students,这种情况使用
< collection property=“students” ofType=“Student”>

<select id="getTeacher" resultMap="getTeacherStudent">
//使用的显式内连接查询
        select t.*,s.id sid,s.name sname
        from
            teacher t
        join
            student s
        on
            t.id=s.tid and t.id=#{id};
    </select>
    <resultMap id="getTeacherStudent" type="teacher">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <collection property="students" ofType="student">
            <id property="id" column="sid"/>
            <result property="name" column="sname"/>
        </collection>
    </resultMap>

子查询 在业务中子查询效率较低
在子查询中只想用一个查询语句可以使用懒加载

动态SQL

where 和 if 一起使用的:
每个if中必须有test,test里面的内容是判断语句
where可以自动去除前面的and

<select id="getUserIf" resultType="user">
        select * from user1
        <where>
            <if test="name != null">//每个if中必须有test,test里面的内容是判断语句
                and name = #{name}//where可以自动去除前面的and
            </if>
            <if test="id != null">
                and id = #{id}
            </if>
        </where>
    </select>

set的使用
set可以自动去除后面的逗号

<update id="updateUser" parameterType="User">
        update user1
        <set>//set可以自动去除后面的逗号
            <if test="id != null and id != ''">
                id=#{id},
            </if>
            <if test="name != null and name != ''">
                name=#{name},
            </if>
            <if test="pwd != null and pwd != ''">
                pwd=#{pwd},
            </if>
        </set>
        where id=#{id}
    </update>

foreach
< foreach collection=“list” item=“item” open="(" separator="," close=")">
collection:指定要遍历集合的key(参数是List,则会自动为一个key为list的map,所以List直接就写list,如果要改变值,可以在dao定义中@param(“指定参数”))
item:遍历的元素名称。如果是map则成为了map中的value

   <select id="getuserforeach" resultType="User">
        select * from user1
        <where>
            id in
            <foreach collection="list" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </where>
    </select>

trim的使用
官网查看具体使用方法

缓存

1级缓存 mybatis自带开启 在一次会话(sqlsession)中
2级缓存,需要在配置文件中设置开启,并且实体类要序列化,还要在mapper.xml配置开启二级缓存,二级缓存是在同一个namespace中,一般使用第三方缓存框架,实现cache接口。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值