Commit b670cd8e authored by 刘凯's avatar 刘凯

1

parent b8ac6908
...@@ -191,6 +191,8 @@ ...@@ -191,6 +191,8 @@
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<!-- Test dependencies --> <!-- Test dependencies -->
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
...@@ -204,6 +206,11 @@ ...@@ -204,6 +206,11 @@
<version>2.3.2</version> <version>2.3.2</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
<dependency> <dependency>
<groupId>org.apache.derby</groupId> <groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId> <artifactId>derby</artifactId>
...@@ -216,6 +223,11 @@ ...@@ -216,6 +223,11 @@
<version>1.10.8</version> <version>1.10.8</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.21.0-GA</version>
</dependency>
<dependency> <dependency>
<groupId>commons-dbcp</groupId> <groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId> <artifactId>commons-dbcp</artifactId>
...@@ -234,6 +246,12 @@ ...@@ -234,6 +246,12 @@
<version>1.7</version> <version>1.7</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>ognl</groupId>
<artifactId>ognl</artifactId>
<version>3.2</version>
</dependency>
<!-- postgresql driver is required to run the refcursor tests --> <!-- postgresql driver is required to run the refcursor tests -->
<dependency> <dependency>
<groupId>postgresql</groupId> <groupId>postgresql</groupId>
......
package com.liukai.demo;
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 java.io.IOException;
import java.io.InputStream;
/**
* @author liukai
* @Description: TODO
* @date 2020/2/254:41 下午
*/
public class Main {
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();
}
}
}
package com.liukai.demo;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;
import org.apache.log4j.Logger;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* @author liukai
* @Description: TODO
* @date 2020/2/254:41 下午
*/
@MappedTypes({String.class})
@MappedJdbcTypes(JdbcType.VARCHAR)
public class MyStringHandler implements TypeHandler<String> {
private Logger log=Logger.getLogger(MyStringHandler.class);
@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);
}
}
package com.liukai.demo;
/**
* @author liukai
* @Description: TODO
* @date 2020/2/254:44 下午
*/
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;
}
}
package com.liukai.demo;
/*
* @author gethin
*/
public interface RoleMapper {
public Role getRole(Long id);
public Role findRole(String roleName);
public int deleteRole(Long id);
public int insertRole(Role role);
}
<?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.liukai.demo.RoleMapper">
<resultMap type="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="com.liukai.demo.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.liukai.demo.MyStringHandler},'%')
</select>
<insert id="insertRole" parameterType="role">
insert into
role(role_name,note) value(#{roleName},#{note})
</insert>
<delete id="deleteRole" parameterType="long">
delete from role where
id=#{id}
</delete>
</mapper>
\ No newline at end of file
log4j.rootLogger=DEBUG,stdout
log4j.logger.org.mybatis=DUBUG
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.logDailyFile.layout.ConversionPattern = %5p %d %C:%m%n
<?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>
<!--<settings>
&lt;!&ndash; <setting name="lazyLoadingEnabled" value="true"/>&ndash;&gt;
&lt;!&ndash; <setting name="aggressiveLazyLoading" value="false"/> &ndash;&gt;
</settings>-->
<typeAliases>
<typeAlias alias="role" type="com.liukai.demo.Role"/>
</typeAliases>
<typeHandlers>
<typeHandler jdbcType="VARCHAR" javaType="string" handler="com.liukai.demo.MyStringHandler"/>
</typeHandlers>
<!-- 定义数据库的信息,默认使用development数据库构建环境 -->
<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://dev.config.duibar.com:3306/test" />
<property name="username" value="dev" />
<property name="password" value="dev_fas015" />
</dataSource>
</environment>
</environments>
<!-- 定义映射器 -->
<mappers>
<package name="com.liukai.demo"/>
</mappers>
</configuration>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment