项目整体结构:
1、新建web工程
2、导入 mybatis-3.2.3.jar和odbc-1.4.jar(数据库包)
mybatis包路径下载:http://pan.baidu.com/s/1nt4xMdz
mybatis学习手册下载:http://pan.baidu.com/s/1nt4xMdz
3、新建数据库配置文件 jdbc.properties
#jdbc properties
db.driver=oracle.jdbc.driver.OracleDriver
db.url=jdbc:oracle:thin:@172.16.11.81:1521:orcl
db.username=gp1
db.password=gp1
4、建立一个mybatis配置文件 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>
<!-- 数据库连接信息 -->
<properties resource="jdbc.properties"></properties>
<!-- 给在*Mapper.xml中sql语句中parameterType resultType 起个别名-->
<typeAliases>
<typeAlias alias="PagerBean" type="com.ifly.qxgl.util.PagerBean" />
<typeAlias alias="Dictionary" type="com.ifly.qxgl.pojo.Dictionary" />
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${db.driver}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
</dataSource>
</environment>
</environments>
<!-- 将*Mapper.xml引入到此 -->
<mappers>
<mapper resource="mybatis/DictionaryMapper.xml" />
</mappers>
</configuration>
5、映射文件(DictionaryMapper.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="com.ifly.qxgl.dao.DictionaryDao">
<sql id="dictionaryLabels">
<![CDATA[
select id, dictype, code, value, remark, seq from tb_dictionary
]]>
</sql>
<select id="findDictionarybyId" parameterType="long" resultType="Dictionary">
<include refid="dictionaryLabels" />
<![CDATA[
where id=#{id}
]]>
</select>
<select id="findDictionaryPager" parameterType="PagerBean" resultType="Dictionary">
<![CDATA[
select * from (select row_.*, rownum rownum_ from (
]]>
<include refid="dictionaryLabels" />
<![CDATA[
where 1=1
]]>
<if test="param.dictype!=null"> and dictype like '%'||#{param.dictype}||'%'</if>
<![CDATA[
) row_ ) where rownum_ <= (#{page}*#{pageSize}) and rownum_ >((#{page}-1)*#{pageSize})
]]>
</select>
<select id="findDictionaryTotal" parameterType="PagerBean"
resultType="long">
<![CDATA[
select count(*) from tb_dictionary
where 1=1
]]>
<if test="param.dictype!=null"> and dictype like '%'||#{param.dictype}||'%'</if>
</select>
<insert id="insertDictionary" parameterType="Dictionary">
<![CDATA[
insert into tb_dictionary
(
]]>
<if test="dictype!=null">dictype,</if>
<if test="code!=null">code,</if>
<if test="value!=null">value,</if>
<if test="remark!=null">remark,</if>
<if test="seq>0">seq,</if>
<![CDATA[
id ) values (
]]>
<if test="dictype!=null">#{dictype},</if>
<if test="code!=null">#{code},</if>
<if test="value!=null">#{value},</if>
<if test="remark!=null">#{remark},</if>
<if test="seq>0">#{seq},</if>
<![CDATA[
seq_dictionary.nextval )
]]>
</insert>
<update id="updateDictionary" parameterType="Dictionary">
<![CDATA[
update tb_dictionary
set
]]>
<if test="dictype!=null">dictype = #{dictype},</if>
<if test="code!=null"> code = #{code},</if>
<if test="value!=null">value = #{value},</if>
<if test="remark!=null">remark = #{remark},</if>
<if test="seq>0">seq = #{seq},</if>
<![CDATA[
id = #{id}
where id = #{id}
]]>
</update>
<delete id="delById" parameterType="long">
<![CDATA[
delete tb_dictionary where id = #{id}
]]>
</delete>
</mapper>
6、pojo(Dictionary.java)
package com.ifly.qxgl.pojo;
import java.io.Serializable;
import com.alibaba.fastjson.JSON;
public class Dictionary implements Serializable {
/**
*
*/
private Long id;
/**
*
*/
private String dictype;
/**
*
*/
private String code;
/**
*
*/
private String value;
/**
*
*/
private String remark;
/**
*
*/
private Integer seq;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getDictype() {
return dictype;
}
public void setDictype(String dictype) {
this.dictype = dictype;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public Integer getSeq() {
return seq;
}
public void setSeq(Integer seq) {
this.seq = seq;
}
@Override
public String toString() {
return JSON.toJSONString(this);
}
}
7、Dao接口(DictionaryDao.java)
package com.ifly.qxgl.dao;
import java.util.List;
import com.ifly.qxgl.pojo.Dictionary;
import com.ifly.qxgl.util.PagerBean;
public interface DictionaryDao {
Dictionary findDictionarybyId(long id);
List<Dictionary> findDictionaryPager(PagerBean<Dictionary> pager);
long findDictionaryTotal(PagerBean<Dictionary> pager);
void insertDictionary(Dictionary dictionary);
void updateDictionary(Dictionary dictionary);
void delById(long id);
}
8、封装获得SqlSessionFactory类(MySessionFactory.java)
package com.ifly.qxgl.util;
import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MySessionFactory {
/**
* SqlSessionFactory
*/
private static SqlSessionFactory sf;
public static SqlSessionFactory getSessionFactory() {
try {
if (sf == null) {
// 使用reader读取xml配置文件
Reader reader = Resources.getResourceAsReader("mybatis.xml");
sf = new SqlSessionFactoryBuilder().build(reader);
}
return sf;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
9、Dao接口测试类(DictionaryDaoTest.java)
package com.ifly.qxgl.dao;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Before;
import org.junit.Test;
import com.ifly.qxgl.pojo.Dictionary;
import com.ifly.qxgl.util.MySessionFactory;
import com.ifly.qxgl.util.PagerBean;
public class DictionaryDaoTest {
private SqlSessionFactory sf;
@Before
public void init() {
sf = MySessionFactory.getSessionFactory();
}
@Test
public void testFindDictionarybyId() {
SqlSession session = sf.openSession();
DictionaryDao dao = (DictionaryDao) session
.getMapper(DictionaryDao.class);
Dictionary dictionary = dao.findDictionarybyId(1);
session.close();
System.out.println(dictionary);
}
@Test
public void testFindDictionaryPager() {
SqlSession session = sf.openSession();
DictionaryDao dao = (DictionaryDao) session
.getMapper(DictionaryDao.class);
PagerBean<Dictionary> pager = new PagerBean<Dictionary>();
Dictionary param = new Dictionary();
param.setDictype("enable");
pager.setPage(1);
pager.setPageSize(10);
pager.setParam(param);
List<Dictionary> dicLists = dao.findDictionaryPager(pager);
session.close();
System.out.println(dicLists);
}
@Test
public void testFindDictionaryTotal() {
SqlSession session = sf.openSession();
DictionaryDao dao = (DictionaryDao) session
.getMapper(DictionaryDao.class);
PagerBean<Dictionary> pager = new PagerBean<Dictionary>();
Dictionary param = new Dictionary();
param.setDictype("enable");
pager.setPage(1);
pager.setPageSize(10);
pager.setParam(param);
long total = dao.findDictionaryTotal(pager);
session.close();
System.out.println(total);
}
@Test
public void testInsertDictionary() {
SqlSession session = sf.openSession();
DictionaryDao dao = (DictionaryDao) session
.getMapper(DictionaryDao.class);
Dictionary dictionary = new Dictionary();
dictionary.setDictype("sex");
dictionary.setCode("96");
dictionary.setValue("男男");
dictionary.setRemark("男男");
dictionary.setSeq(500);
dao.insertDictionary(dictionary);
session.commit();
session.close();
}
@Test
public void testUpdateDictionary() {
SqlSession session = sf.openSession();
DictionaryDao dao = (DictionaryDao) session
.getMapper(DictionaryDao.class);
Dictionary dictionary = new Dictionary();
dictionary.setId((long) 41);
dictionary.setDictype("sex");
dictionary.setCode("961");
dictionary.setValue("男男1");
dictionary.setRemark("男男1");
dictionary.setSeq(500);
dao.updateDictionary(dictionary);
session.commit();
session.close();
}
@Test
public void testDelById() {
SqlSession session = sf.openSession();
DictionaryDao dao = (DictionaryDao) session
.getMapper(DictionaryDao.class);
dao.delById((long) 41);
session.commit();
session.close();
}
}
10、Service层编写(DictionaryService.java)
package com.ifly.qxgl.service;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import com.ifly.qxgl.dao.DictionaryDao;
import com.ifly.qxgl.pojo.Dictionary;
import com.ifly.qxgl.util.MySessionFactory;
import com.ifly.qxgl.util.PagerBean;
public class DictionaryService {
private SqlSessionFactory sf;
public DictionaryService() {
sf = MySessionFactory.getSessionFactory();
}
public PagerBean<Dictionary> findDictionaryPager(PagerBean<Dictionary> page) {
SqlSession session = sf.openSession();
try {
DictionaryDao dao = (DictionaryDao) session
.getMapper(DictionaryDao.class);
List<Dictionary> dicLists = dao.findDictionaryPager(page);
long total = dao.findDictionaryTotal(page);
page.setRows(dicLists);
page.setTotal(total);
} catch (Exception e) {
} finally {
session.close();
}
return page;
}
}
11、service的测试类(DictionaryServiceTest.java)
package com.ifly.qxgl.service;
import org.junit.Test;
import com.ifly.qxgl.pojo.Dictionary;
import com.ifly.qxgl.util.PagerBean;
public class DictionaryServiceTest {
@Test
public void testFindDictionaryPager() {
DictionaryService dictionaryService = new DictionaryService();
PagerBean<Dictionary> pager = new PagerBean<Dictionary>();
pager.setPage(1);
pager.setPageSize(10);
Dictionary param = new Dictionary();
param.setDictype("enable");
pager.setParam(param);
pager = dictionaryService.findDictionaryPager(pager);
System.out.println(pager);
}
}
12、分页工具类(PagerBean.java)
package com.ifly.qxgl.util;
import java.util.List;
import com.alibaba.fastjson.JSONObject;
/**
* 分页
*/
public class PagerBean<T> {
/**
* 默认当前页
*/
private int page;
/**
* 每页大小
*/
private int pageSize;
/**
* 查询的结果总数
*/
private long total;
/**
* 对象查询参数
*/
private T param;
/**
* 每页的分页数据
*/
private List<T> rows;
/**
* 总页数
*/
private int totalPage;
/**
* @return the page
*/
public int getPage() {
return page;
}
/**
* @param page
* the page to set
*/
public void setPage(int page) {
this.page = page;
}
/**
* @return the pageSize
*/
public int getPageSize() {
return pageSize;
}
/**
* @param pageSize
* the pageSize to set
*/
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
/**
* @return the total
*/
public long getTotal() {
return total;
}
/**
* @param total
* the total to set
*/
public void setTotal(long total) {
this.total = total;
}
/**
* @return the rows
*/
public List<T> getRows() {
return rows;
}
/**
* @param rows
* the rows to set
*/
public void setRows(List<T> rows) {
this.rows = rows;
}
public int getTotalPage() {
this.totalPage = (int) this.total % this.pageSize != 0 ? (int) this.total
/ this.pageSize + 1
: (int) this.total / this.pageSize;
return totalPage;
}
public T getParam() {
return param;
}
public void setParam(T param) {
this.param = param;
}
@Override
public String toString() {
return JSONObject.toJSONString(this);
}
}
13、数据库表
14、源码下载:
链接: http://pan.baidu.com/s/1o6yqgL8 密码: tql7