引入包及前期工作,见上篇博文:mybatis 入门HelloWorld
这次演示对setting表的增删改查:
数据库结构图:
Setting.java类
import java.io.Serializable;
import cn.kepu.banche.common.DBTable;
/**
* db system table
*
* @author dell
*
*/
@DBTable(name="setting")
public class Setting implements Serializable {
private static final long serialVersionUID = 2093445679662874097L;
private int id;
private String version;
private String apkUrl;
private String content;
public Setting(int id, String version, String apkUrl, String content) {
super();
this.id = id;
this.version = version;
this.apkUrl = apkUrl;
this.content = content;
}
public Setting() {
super();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getApkUrl() {
return apkUrl;
}
public void setApkUrl(String apkUrl) {
this.apkUrl = apkUrl;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "setting:[id='"+getId()+"',version='"+getVersion()+"',apkUrl='"+getApkUrl()+"',content='"+getContent()+"']";
}
}
SettingMapper.xml
<?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="Setting">
<!-- 如果表字段和javaBean名称不一样,可以通过这样的映射来解决 -->
<resultMap type="cn.kepu.banche.model.Setting" id="setting">
<result property="id" column="id" />
<result property="version" column="version" />
<result property="apkUrl" column="apkUrl" />
<result property="content" column="content" />
</resultMap>
<!-- 直接通过map或者javabean插入数据 -->
<insert id="insert" parameterType="cn.kepu.banche.model.Setting">
insert into setting(version,apkUrl,content) values( #{version},
#{apkUrl}, #{content} )
</insert>
<!-- 更新 -->
<update id="update" parameterType="cn.kepu.banche.model.Setting">
update setting
<set>
<if test="version!=null">version=#{version},</if>
<if test="apkUrl!=null">apkUrl=#{apkUrl},</if>
<if test="content!=null">content=#{content}</if>
</set>
<where>
<if test="id!=null">id=#{id}</if>
</where>
</update>
<!-- 删除 -->
<delete id="delete" parameterType="Map">
delete from setting
<where>
<if test="id!=null">id=#{id}</if>
</where>
</delete>
<!-- 查询总数据条数 -->
<select id="selectCount" parameterType="Map" resultType="int">
select count(*) from setting
</select>
<!-- 按条件查询 -->
<select id="select" resultMap="setting" parameterType="Map">
select * from setting
<include refid="selectOption" />
</select>
<!-- 多条件查询语句 -->
<sql id="selectOption">
<where>
<if test="id!=null">id=#{id}</if>
<if test="version!=null">version=#{version},</if>
<if test="apkUrl!=null">apkUrl=#{apkUrl}</if>
<if test="content!=null">content=#{content}</if>
</where>
</sql>
</mapper>
mybatis.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>
<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://127.0.0.1:3306/banche"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/SettingMapper.xml"/>
</mappers>
</configuration>
Log4J.properties:
#DEBUG,INFO,
log4j.rootLogger=DEBUG,ConsoleLog
log4j.appender.ConsoleLog=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Target=System.out
log4j.appender.ConsoleLog.layout=org.apache.log4j.PatternLayout
#log4j.appender.ConsoleLog.layout.ConversionPattern=[%-5p]%d{yyyy-MM-dd HH\:mm\:ss,SSS}%n%m%n%n
log4j.appender.ConsoleLog.layout.ConversionPattern=%t [%-5p] [%d{yyyy-MM-dd HH\:mm\:ss,SSS}] %40l - %m%n
测试类:
package cn.kepu.banche.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import cn.kepu.banche.model.Setting;
public class TestSettingDao {
public static void main(String[] args) throws IOException {
String resource = "mybatis.xml";
InputStream in = Resources.getResourceAsStream(resource);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
// 插入
// HashMap<String, String> map = new HashMap<String, String>();
// map.put("version", "V1.2");
// map.put("apkurl", "www.www");
// map.put("content", "V1.2_www_www");
// System.out.println("通过map插入结果:" + insert(factory.openSession(), map));
// 插入
// Setting setting = new Setting();
// setting.setVersion("V1.3");
// setting.setApkUrl("www.www.www");
// setting.setContent("V1.3_www_www");
// System.out.println("通过map插入结果:" + insert(factory.openSession(), setting));
//更新
// Setting setting = new Setting();
// setting.setId(2);
// setting.setApkUrl("uu_www.www.www");
// System.out.println("通过map更新结果:" + update(factory.openSession(), setting));
//删除
// Setting deleteSetting = new Setting();
// deleteSetting.setId(2);
// System.out.println("通过map删除结果:" + delete(factory.openSession(), deleteSetting));
//查询
List<Setting> settings = find(factory.openSession(), null);
for(Setting s : settings){
System.out.println(s);
}
}
//插入
private static boolean insert(SqlSession sqlSession, Object mapOrJavaBean) {
int i = sqlSession.insert("Setting.insert", mapOrJavaBean);
sqlSession.commit();
return i > 0;
}
//更新
private static boolean update(SqlSession sqlSession, Object mapOrJavaBean) {
int i = sqlSession.update("Setting.update", mapOrJavaBean);
sqlSession.commit();
return i > 0;
}
//删除
private static boolean delete(SqlSession sqlSession, Object mapOrJavaBean){
int i = sqlSession.delete("Setting.delete", mapOrJavaBean);
sqlSession.commit();
return i > 0;
}
//查询
private static List<Setting> find(SqlSession sqlSession, Object mapOrJavaBean){
List<Setting> settings = sqlSession.selectList("Setting.select");
return settings;
}
}
运行结果示例:
main [DEBUG] [2013-03-04 09:46:14,892] org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.debug(JakartaCommonsLoggingImpl.java:46) - Logging initialized using 'class org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl' adapter.
main [DEBUG] [2013-03-04 09:46:14,939] org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.debug(JakartaCommonsLoggingImpl.java:46) - PooledDataSource forcefully closed/removed all connections.
main [DEBUG] [2013-03-04 09:46:14,939] org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.debug(JakartaCommonsLoggingImpl.java:46) - PooledDataSource forcefully closed/removed all connections.
main [DEBUG] [2013-03-04 09:46:14,939] org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.debug(JakartaCommonsLoggingImpl.java:46) - PooledDataSource forcefully closed/removed all connections.
main [DEBUG] [2013-03-04 09:46:14,939] org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.debug(JakartaCommonsLoggingImpl.java:46) - PooledDataSource forcefully closed/removed all connections.
main [DEBUG] [2013-03-04 09:46:15,048] org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.debug(JakartaCommonsLoggingImpl.java:46) - Openning JDBC Connection
main [DEBUG] [2013-03-04 09:46:15,252] org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.debug(JakartaCommonsLoggingImpl.java:46) - Created connection 8442367.
main [DEBUG] [2013-03-04 09:46:15,252] org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.debug(JakartaCommonsLoggingImpl.java:46) - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@80d1ff]
main [DEBUG] [2013-03-04 09:46:15,252] org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.debug(JakartaCommonsLoggingImpl.java:46) - ==> Preparing: select * from setting
main [DEBUG] [2013-03-04 09:46:15,283] org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.debug(JakartaCommonsLoggingImpl.java:46) - ==> Parameters:
setting:[id='1',version='V1.1',apkUrl='www',content='null']
setting:[id='3',version='V1.3',apkUrl='www.www.www',content='V1.3_www_www']
setting:[id='4',version='V1.3',apkUrl='www.www.www',content='V1.3_www_www']
setting:[id='5',version='V1.3',apkUrl='www.www.www',content='V1.3_www_www']
setting:[id='6',version='V1.3',apkUrl='www.www.www',content='V1.3_www_www']
项目结构图