mybaits学习笔记(一)

1,包:commons-logging.jar
log4j-1.2.15.jar
mybatis-3.2.2.jar

mysql-connector-java-5.1.7-bin.jar

2mybaits整体配置文件:src下configuration.xml

<?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>
<properties resource="db.properties"></properties>
<typeAliases>
<package name="bean"/><!-- com.zrgk.entity -->
<!-- <package name="com.zrgk.entity />" -->
</typeAliases>
<!-- 引入资源文件 -->
<environments default="development"><!-- 开发模式 default="work" 工作模式 -->
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<mappers><!-- 引入映射配置文件 -->
<mapper resource="bean/UserMapper.xml" />
</mappers>
</configuration>

3.映射配置文件:bean下UserMappering

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




<mapper namespace="bean.UserMapper">
<!-- namespace 必须加实体类的映射文件
(com.zrgk.entity.UserMapper)的所在路径-->

<select id="selectUserById" parameterType="int" resultType="bean.User">
select * from user where id=#{id}
</select>
<delete id="deleteUserById" parameterType="int"  >
delete from user where id=#{id} 
</delete>
<update id="updateUserByid" parameterType="bean.User" >
update user set name=#{name},age=#{age},birthday=#{birthday} where id=#{id}
</update>
<insert id="insertUser" parameterType="bean.User">
insert into  user(name,age,birthday) values(#{name},#{age},#{birthday})
</insert>
<select id="selectUserByName" parameterType="string" resultType="bean.User">
select * from user where name like "%"#{name}"%"
</select>
<select id="selectUserByCondition" parameterType="bean.User" resultType="bean.User">
select * from user where 1=1
<if test="name!=null and name!='' ">
and name like "%"#{name}"%"
</if>
<if test="age>0 ">
and age=#{age}
</if>
<if test="birthday!=null and birthday!='' ">
and birthday =#{birthday}
</if>
</select>

<select id="selectUserByCondition2" parameterType="bean.User" resultType="bean.User">

select * from user
<where>
<if test="name!=null and name!='' ">
name like "%"#{name}"%"
</if>
<if test="age>0 ">
and  age=#{age}
</if>
<if test="birthday!=null and birthday!='' ">
and birthday =#{birthday}
</if>
</where>

</select>

<update id="updateUserDyna" >
update user
<set>
<if test="name!=null">
name=#{name},
</if>
<if test="age>0">
age=#{age},
</if>
<if test="birthday!=null">
birthday=#{birthday}
</if>
</set>
where id=#{id}
</update>
<!-- 动态查询foreach -->
<select id="dynaForeach" parameterType="java.util.List" resultType="bean.User">
select * from user where id in 
<foreach collection="list" item="item" index="index" open="(" separator="," close=")">
#{item}
</foreach>
</select>
<!-- 动态查询foreach -->
<select id="dynaForeach2"  resultType="bean.User">
select * from user where id in 
<foreach collection="array" item="item" index="index" open="(" separator="," close=")">
#{item}
</foreach>
</select>
<!-- 动态查询foreach -->
<select id="dynaForeach3" parameterType="java.util.Map" resultType="bean.User">
select * from user where name like "%"#{name}"%" and id in
<foreach collection="ids" item="item" index="index" open="(" separator="," close=")">
#{item}
</foreach>
</select>


</mapper>    

4.SqlSessionFactory工具类:

package util;


import java.io.Reader;


import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;


public class MyBatisUtil {


private static SqlSessionFactory factory;
private static Reader reader;
static{
try{
reader = Resources.getResourceAsReader("configuration.xml");

}catch (Exception e) {
e.printStackTrace();
}

}
public static SqlSessionFactory getSessionFactory(){
if(factory==null){
factory=new SqlSessionFactoryBuilder().build(reader);

}
return factory;
}
public static SqlSession getSession(){
return getSessionFactory().openSession();
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值