怎么给别人做网站优化,维普网,淘宝网站建设设计模板,社区营销模式文章目录 源码下载测试模块搭建学习博客 源码下载
首先下载mybatis-parent的源码#xff1a;gitee地址 https://gitee.com/callback_lab/mybatis-parent.git
然后下载mybatis的源码#xff1a;gitee地址 https://gitee.com/callback_lab/mybatis-src.git
带中文… 文章目录 源码下载测试模块搭建学习博客 源码下载
首先下载mybatis-parent的源码gitee地址 https://gitee.com/callback_lab/mybatis-parent.git
然后下载mybatis的源码gitee地址 https://gitee.com/callback_lab/mybatis-src.git
带中文注释的三方源码 以下包需要注释否则会报错
org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### Cause: java.lang.IllegalStateException: Cannot enable lazy loading because Javassist is not available. Add Javassist to your classpath.dependencygroupIdognl/groupIdartifactIdognl/artifactIdversion3.2.20/version
!-- scopecompile/scope--
!-- optionaltrue/optional--/dependencydependencygroupIdorg.javassist/groupIdartifactIdjavassist/artifactIdversion3.27.0-GA/version
!-- scopecompile/scope--
!-- optionaltrue/optional--/dependency将mybatis-parent与mybatis导入idea同一个project下。
测试模块搭建
新建一个测试模块这里叫mybatis-gabriel
项目基础架构
实例原博客
pom : dependenciesdependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.11/versionscopetest/scope/dependencydependencygroupIdorg.mybatis/groupIdartifactIdmybatis/artifactIdversion3.3.0-SNAPSHOT/version/dependency/dependencies相关代码
主要测试入口代码
public class TestMain {public static void main(String[] args) {String resource mybatis-config.xml;InputStream inputStream null;try {inputStream Resources.getResourceAsStream(resource);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}SqlSessionFactory sqlSessionFactory null;sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream);SqlSession sqlSession null;try {sqlSession sqlSessionFactory.openSession();RoleMapper roleMapper sqlSession.getMapper(RoleMapper.class);Role role roleMapper.getRole(1L);System.out.println(role.getId() : role.getRoleName() : role.getNote());sqlSession.commit();} catch (Exception e) {// TODO Auto-generated catch blocksqlSession.rollback();e.printStackTrace();} finally {sqlSession.close();}}
}po
/** author gethin* 角色的实体类*/
public class Role {private long id;private String roleName;private String note;public long getId() {return id;}public void setId(long id) {this.id id;}public String getRoleName() {return roleName;}public void setRoleName(String roleName) {this.roleName roleName;}public String getNote() {return note;}public void setNote(String note) {this.note note;}
}MyStringHandler
MappedTypes({String.class})
MappedJdbcTypes(JdbcType.VARCHAR)
public class MyStringHandler implements TypeHandlerString {Logger log Logger.getLogger(MyStringHandler.class.getName());Overridepublic String getResult(ResultSet rs, String colName) throws SQLException {log.info(使用我的TypeHandler,ResultSet列名获取字符串);return rs.getString(colName);}Overridepublic String getResult(ResultSet rs, int index) throws SQLException {log.info(使用我的TypeHandler,ResultSet下标获取字符串);return rs.getString(index);}Overridepublic String getResult(CallableStatement cs, int index) throws SQLException {log.info(使用我的TypeHandler,CallableStatement下标获取字符串);return cs.getString(index);}Overridepublic void setParameter(PreparedStatement ps, int index, String value, JdbcType arg3) throws SQLException {log.info(使用我的TypeHandler);ps.setString(index, value);}}mapper
public interface RoleMapper {public Role getRole(Long id);public Role findRole(String roleName);public int deleteRole(Long id);public int insertRole(Role role);
}RoleMapper.xml
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespaceorg.mybatis.debug.mapper.RoleMapperresultMap typeorg.mybatis.debug.po.Role idroleMapid columnid propertyid javaTypelong jdbcTypeBIGINT /result columnrole_name propertyroleName javaTypestringjdbcTypeVARCHAR /result columnnote propertynotetypeHandlerorg.mybatis.debug.handle.MyStringHandler //resultMapselect idgetRole parameterTypelong resultMaproleMapselectid,role_name as roleName,note from role where id#{id}/selectselect idfindRole parameterTypelong resultMaproleMapselectid,role_name,note from role where role_name like CONCAT(%,#{roleNamejavaTypestring,jdbcTypeVARCHAR,typeHandlercom.gethin.handler.MyStringHandler},%)/selectinsert idinsertRole parameterTypeorg.mybatis.debug.po.Roleinsert intorole(role_name,note) value(#{roleName},#{note})/insertdelete iddeleteRole parameterTypelongdelete from role whereid#{id}/delete
/mappersql
-- ----------------------------
-- Table structure for role
-- ----------------------------
DROP TABLE IF EXISTS role;
CREATE TABLE role (id int(11) DEFAULT NULL,role_name varchar(255) DEFAULT NULL,note varchar(255) DEFAULT NULL
) ENGINEInnoDB DEFAULT CHARSETutf8;-- ----------------------------
-- Records of role
-- ----------------------------
INSERT INTO role VALUES (1, 管理员, 管理员);-- ----------------------------
-- Table structure for role
-- ----------------------------
DROP TABLE IF EXISTS user;
CREATE TABLE user (id int(11) DEFAULT NULL,role_id int(11) DEFAULT NULL,user_name varchar(255) DEFAULT NULL,user_note varchar(255) DEFAULT NULL
) ENGINEInnoDB DEFAULT CHARSETutf8;-- ----------------------------
-- Records of role
-- ----------------------------
INSERT INTO user VALUES (1, 1,张三, 管理员张三);学习博客
调试博客