首先下载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.
<dependency>
<groupId>ognlgroupId>
<artifactId>ognlartifactId>
<version>3.2.20version>
dependency>
<dependency>
<groupId>org.javassistgroupId>
<artifactId>javassistartifactId>
<version>3.27.0-GAversion>
dependency>
将mybatis-parent与mybatis导入idea同一个project下。
新建一个测试模块,这里叫mybatis-gabriel
实例原博客
pom :
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.3.0-SNAPSHOTversion>
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 block
e.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 block
sqlSession.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 TypeHandler<String> {
Logger log= Logger.getLogger(MyStringHandler.class.getName());
@Override
public String getResult(ResultSet rs, String colName) throws SQLException {
log.info("使用我的TypeHandler,ResultSet列名获取字符串");
return rs.getString(colName);
}
@Override
public String getResult(ResultSet rs, int index) throws SQLException {
log.info("使用我的TypeHandler,ResultSet下标获取字符串");
return rs.getString(index);
}
@Override
public String getResult(CallableStatement cs, int index) throws SQLException {
log.info("使用我的TypeHandler,CallableStatement下标获取字符串");
return cs.getString(index);
}
@Override
public 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 :
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.mybatis.debug.mapper.RoleMapper">
<resultMap type="org.mybatis.debug.po.Role" id="roleMap">
<id column="id" property="id" javaType="long" jdbcType="BIGINT" />
<result column="role_name" property="roleName" javaType="string"
jdbcType="VARCHAR" />
<result column="note" property="note"
typeHandler="org.mybatis.debug.handle.MyStringHandler" />
resultMap>
<select id="getRole" parameterType="long" resultMap="roleMap">
select
id,role_name as roleName,note from role where id=#{id}
select>
<select id="findRole" parameterType="long" resultMap="roleMap">
select
id,role_name,note from role where role_name like CONCAT('%',#{roleName
javaType=string,
jdbcType=VARCHAR,typeHandler=com.gethin.handler.MyStringHandler},'%')
select>
<insert id="insertRole" parameterType="org.mybatis.debug.po.Role">
insert into
role(role_name,note) value(#{roleName},#{note})
insert>
<delete id="deleteRole" parameterType="long">
delete from role where
id=#{id}
delete>
mapper>
sql :
-- ----------------------------
-- 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
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- 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
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of role
-- ----------------------------
INSERT INTO `user` VALUES ('1', '1','张三', '管理员张三');
调试博客