您的位置:首页 > 大数据 > 人工智能

mybaits学习笔记(一)

2015-12-06 03:10 381 查看
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">
a466
;
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();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: