自MYBATIS从BATIS奔向Google后,一直在蜕化,大方向实现分xml和注解Annation。接下来分别为初学者推出xml和注解Annation两种Demo。
一、第一种实现方式:xml
1、Demo结构。
2、配置文件
(1)数据源:jdbc.properties
#******************** 数据库连接属性 *****************************
driver=com.mysql.jdbc.Driver
username=root
password=123
url=jdbc\:mysql\://localhost\:3306/solrdb?characterEncoding\=utf8
(2)mybatis-config.xml配置
(3)日志文件:log4j.xml
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
(4)sysUser.xml配置
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
(1)LogHelper.java
package com.wrs.common;import java.net.URL;import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;/*** @function 项目日志 * @author WRS* @remark 项目日志打印:常用日志、sql日志 exception异常日志* @version 1.0* @since jdk1.8* @datetime 2015年7月9日 下午4:10:39* @copyright {xx.com (c) 2018}*/
public class LogHelper {static{URL configFileResource = LogHelper.class.getResource("/log4j.xml");PropertyConfigurator.configure(configFileResource);}/*** 普通日志打印* @return 返回普通日志对象*/public static Logger printLog() {return Logger.getLogger("log");}/*** sql日志打印* @return 返回sql日志对象*/public static Logger printSql() {return Logger.getLogger("sql");}/*** 异常日志打印* @return 返回exception日志对象*/public static Logger printException() {return Logger.getLogger("exception");}public static void main(String[] args) {}}
SessionFactory.java
package com.wrs.common;import java.io.IOException;
import java.io.InputStream;import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;/*** @function session工厂 * @author {作者}* @remark {描述}* @version {版本}* @since jdk1.6* @datetime 2015年7月28日 上午9:57:58* @copyright {wrs.com (c) 2013}*/
public class SessionFactory {/*** 获取session对象* @return*/public static SqlSession getSession(){LogHelper.printLog().info("SessionFactory getSession()");SqlSession session = null;try {String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);if(sqlSessionFactory!=null){session = sqlSessionFactory.openSession();}else{LogHelper.printLog().info("Session对象创建失败,请检查mybatis-config.xml配置是否正确!");}} catch (IOException e) {e.printStackTrace();}return session;}/*** 释放没用的资源* @param sqlSession*/public static void close(SqlSession session) {LogHelper.printLog().info("SessionFactory close(SqlSession session)");if (session!=null) {//释放session资源session.clearCache();session.close();session = null;}LogHelper.printLog().info("释放资源完毕!");}public static void main(String[] args) {getSession();}
}
数据分层》
SysUserDao.java
package com.wrs.dao;import java.util.List;import com.wrs.model.SysUser;/*** @function {功能} * @author {作者}* @remark {描述}* @version {版本}* @since jdk1.6* @datetime 2015年7月30日 下午1:53:32* @copyright {wrs.com (c) 2013}*/
public interface SysUserDao{/*** 增加用户* @param sysUser* @return*/public int insert(SysUser sysUser);/*** 删除用户* @param id* @return*/public int delete(Integer id);/*** 更新用户* @param sysUser* @return*/public int update(SysUser sysUser);/*** 查询用户* @param id* @return*/public SysUser query(Integer id);/*** 获取所有用户信息* @return*/public List
}
SysUserDaoImpl.java
package com.wrs.dao.impl;import java.util.List;import org.apache.ibatis.session.SqlSession;import com.wrs.common.SessionFactory;
import com.wrs.dao.SysUserDao;
import com.wrs.model.SysUser;/*** @function 用户数据访问接口实现 * @author WRS* @remark 增、删、改、查* @version 1.0* @since jdk1.6* @datetime 2015年7月30日 下午1:58:09* @copyright {wrs.com (c) 2013}*/
public class SysUserDaoImpl implements SysUserDao {private SqlSession session = null;public SysUserDaoImpl() {if (session==null) {session = SessionFactory.getSession();}}/** 新增用户* 【注意:增加时应该将session的自动提交改为手动提交,不执行改操作会导致无法增加数据】* (non-Javadoc)* @see com.wrs.dao.SysUserDao#insert(com.wrs.model.SysUser)*/@Overridepublic int insert(SysUser sysUser) {session.commit(false);//1、先取消自动提交int insertRslt = session.insert("com.wrs.model.SysUserMapper.insertSysUser", sysUser);//2、执行增加操作session.commit();//3、手动提交return insertRslt;}/** 删除用户* 【注意:删除时应该将session的自动提交改为手动提交,不执行改操作会导致无法删除数据】* (non-Javadoc)* @see com.wrs.dao.SysUserDao#delete(java.lang.Integer)*/@Overridepublic int delete(Integer Id) {session.commit(false);int deleteRslt = session.delete("com.wrs.model.SysUserMapper.deleteSysUser", Id);session.commit();System.out.println("删除用户结果:"+(deleteRslt==1?"成功":"失败")+"!");return deleteRslt;}/** 修改用户* 【注意:修改时应该将session的自动提交改为手动提交,不执行改操作会导致无法修改数据】* (non-Javadoc)* @see com.wrs.dao.SysUserDao#update(com.wrs.model.SysUser)*/@Overridepublic int update(SysUser sysUser) {session.commit(false);int deleteRslt = session.update("com.wrs.model.SysUserMapper.updateSysUser", sysUser);session.commit();return deleteRslt;}/** 查询单个用户* (non-Javadoc)* @see com.wrs.dao.SysUserDao#query(java.lang.Integer)*/@Overridepublic SysUser query(Integer Id) {return session.selectOne("com.wrs.model.SysUserMapper.selectSysUser", Id);}/** 查询所有用户* (non-Javadoc)* @see com.wrs.dao.SysUserDao#querySysUserList()*/@Overridepublic List
业务逻辑层》
SysUserService.java
package com.wrs.service;import java.util.List;import com.wrs.model.SysUser;/*** @function {功能} * @author {作者}* @remark {描述}* @version {版本}* @since jdk1.6* @datetime 2015年7月30日 下午1:53:32* @copyright {wrs.com (c) 2013}*/
public interface SysUserService {/*** 增加用户* @param sysUser* @return*/public int insert(SysUser sysUser);/*** 删除用户* @param id* @return*/public int delete(Integer id);/*** 更新用户* @param sysUser* @return*/public int update(SysUser sysUser);/*** 查询用户* @param id* @return*/public SysUser query(Integer id);/*** 获取所有用户信息* @return*/public List
}
SysUserServiceImpl.java
package com.wrs.service.impl;import java.util.List;import com.wrs.dao.SysUserDao;
import com.wrs.dao.impl.SysUserDaoImpl;
import com.wrs.model.SysUser;
import com.wrs.service.SysUserService;/*** @function {功能} * @author {作者}* @remark {描述}* @version {版本}* @since jdk1.6* @datetime 2015年7月30日 下午1:58:09* @copyright {wrs.com (c) 2013}*/
public class SysUserServiceImpl implements SysUserService {private SysUserDao sysUserDao = null;public SysUserServiceImpl() {if (sysUserDao==null) {sysUserDao = new SysUserDaoImpl();}}@Overridepublic int insert(SysUser sysUser) {return sysUserDao.insert(sysUser);}@Overridepublic int delete(Integer Id) {return sysUserDao.delete(Id);}@Overridepublic int update(SysUser sysUser) {return sysUserDao.update(sysUser);}@Overridepublic SysUser query(Integer Id) {return sysUserDao.query(Id);}@Overridepublic List
调用Action》
SysUserAction.java
package com.wrs.action;import java.util.List;import com.wrs.common.LogHelper;
import com.wrs.model.SysUser;
import com.wrs.service.SysUserService;
import com.wrs.service.impl.SysUserServiceImpl;/*** @function {功能} * @author {作者}* @remark {描述}* @version {版本}* @since jdk1.6* @datetime 2015年7月30日 上午11:47:31* @copyright {wrs.com (c) 2013}*/
public class SysUserAction {private SysUserService sysUserService = null;public SysUserAction() {if(sysUserService==null){sysUserService = new SysUserServiceImpl();}}/*** 新增用户* 【注意:增加时应该将session的自动提交改为手动提交,不执行改操作会导致无法增加数据】*/public void insert(){LogHelper.printLog().info("SysUserAction insert()");SysUser sysUser = new SysUser(3,"生哥");//如数据库支持Id主键自增,则Id可以为null值,如"mysql数据库"int insertRslt = sysUserService.insert(sysUser);LogHelper.printLog().info("新增用户结果:"+(insertRslt==1?"成功":"失败")+"!");}/*** 删除用户* 【注意:删除时应该将session的自动提交改为手动提交,不执行改操作会导致无法删除数据】*/public void delete(){LogHelper.printLog().info("SysUserAction delete()");int deleteRslt = sysUserService.delete(3);LogHelper.printLog().info("删除用户结果:"+(deleteRslt==1?"成功":"失败")+"!");}/*** 修改用户* 【注意:删除时应该将session的自动提交改为手动提交,不执行改操作会导致无法修改数据】*/public void update(){LogHelper.printLog().info("SysUserAction delete()");SysUser sysUser = new SysUser(3,"生哥别名");int deleteRslt = sysUserService.update(sysUser);LogHelper.printLog().info("修改用户结果:"+(deleteRslt==1?"成功":"失败")+"!");}/*** 查询用户*/public void query(){LogHelper.printLog().info("SysUserAction query()");SysUser sysUser = sysUserService.query(1);LogHelper.printLog().info("用户ID:"+sysUser.getId()+",用户名:"+sysUser.getUser_name());}/*** 查询所有用户数量*/public void querySysUserList(){LogHelper.printLog().info("SysUserAction query()");List
}
友情提示:本人提供相关IT技术开发和支持,与其相关技术交流。
如需请加微信号: