<configuration>
<properties resource="db.properties">properties>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
<typeAliases>
<package name="com.mybatis.pojo">package>
typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
dataSource>
environment>
environments>
<mappers>
<package name="com.mybatis.mapper">package>
mappers>
configuration>
<select id="selectPerson" parameterType="int" resultType="hashmap">
SELECT * FROM PERSON WHERE ID = #{id}
select>
<insert id="insertAuthor" parameterType="domain.blog.Author">
insert into Author (id,username,password,email,bio)
values (#{id},#{username},#{password},#{email},#{bio})
insert>
<update id="updateAuthor" parameterType="domain.blog.Author">
update Author set
username = #{username},
password = #{password},
email = #{email},
bio = #{bio}
where id = #{id}
update>
<delete id="deleteAuthor" parameterType="int">
delete from Author where id = #{id}
delete>
普通封装
<resultMap type="com.atguigu.mybatis.bean.Employee" id="MyEmp">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="age" property="age"/>
<result column="sex" property="sex"/>
resultMap>
多表联查
<resultMap type="com.test.mybatis.po.Student" id="MySchoolEmp">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="age" property="age"/>
<result column="sex" property="sex"/>
<association property="school" javaType="com.test.mybatis.po.School">
<id column="cId" property="id"/>
<result column="cName" property="name"/>
association>
resultMap>
返回类型包含链表
<resultMap type="com.test.mybatis.po.School" id="myMap1">
<id column="id" property="id"/>
<result column="name" property="name"/>
<collection property="stus" ofType="com.test.mybatis.po.Student">
<id column="sId" property="id"/>
<result column="sName" property="name"/>
<result column="age" property="age"/>
<result column="sex" property="sex"/>
collection>
resultMap>
<select id="getSchoolByIdPlus" resultMap="myMap1">
select sc.id id, sc.name name, st.id sId, st.name sName, st.age age, st.sex sex from school sc left join student st on sc.id = st.s_id where sc.id = #{id}
select>
public class Main {
private SqlSession session;
@Before
public void init(){
String resource = "mybatis-config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory =
new SqlSessionFactoryBuilder().build(inputStream);
this.session = sqlSessionFactory.openSession(true);
//true: 设置自动提交
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void test(){
UserMapper mapper = session.getMapper(UserMapper.class);
//todo
//测试
}
@After
public void destroy(){
session.close();
}
}
<select id="getStudentByConditionIf" resultType="com.test.mybatis.po.Student">
select * from student
<where>
<if test="id != null && id != 0">
id = #{id}
if>
<if test="name != null && name != ''">
and name = #{name}
if>
<if test="age != null">
and age = #{age}
if>
<if test="sex == "男" or sex == "女"">
and sex = #{sex}
if>
where>
select>
<select id="getStudentByConditionTrim" resultType="com.test.mybatis.po.Student">
select * from student
<trim prefix="where" suffixOverrides="and">
<if test="id != null && id != 0">
id = #{id} and
if>
<if test="name != null && name != ''">
name = #{name} and
if>
<if test="age != null">
age = #{age} and
if>
<if test="sex == "男" or sex == "女"">
sex = #{sex}
if>
trim>
select>
trim属性
choose:类似于switch
<select id="getStudentByConditionChoose" resultType="com.test.mybatis.po.Student">
select * from student
<where>
<choose>
<when test="id != null">
id = #{id}
when>
<when test="name != null">
name = #{name}
when>
<when test="age != null">
age = #{age}
when>
<otherwise>
sex = "女"
otherwise>
choose>
where>
select>
set:用于去掉最后的 ,
<update id="setStudent">
update student
<!-- 通过set标签设置 -->
<!-- 通过trim标签设置 -->
<trim prefix="set" suffixOverrides=",">
<if test="name != null && name != ''">
name = #{name},
if>
<if test="age != null">
age = #{age},
if>
<if test="sex == "男" or sex == "女"">
sex = #{sex}
if>
trim>
where id=#{id}
update>
foreach
<select id="getStudentByConditionForeach"
resultType="com.test.mybatis.po.Student">
select * from student
<foreach collection="list" item="item_id" separator="," open="where id in("
close=")">
#{item_id}
foreach>
select>
<insert id="insertStudent">
insert into student(name,age,sex,s_id) values
<foreach collection="stus" item="stu" separator=",">
(#{stu.name},#{stu.age},#{stu.sex},#{stu.school.id})
foreach>
insert>