Commit 5893d87a authored by 沈少华's avatar 沈少华

沈少华作业

parent 1441d50e
mybatis作业
mybatis作业
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="mybatiswork" />
</profile>
</annotationProcessing>
<bytecodeTargetLevel>
<module name="mybatiswork" target="1.8" />
</bytecodeTargetLevel>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="SerializableHasSerialVersionUIDField" enabled="true" level="WARNING" enabled_by_default="true">
<option name="ignoreAnonymousInnerClasses" value="false" />
<option name="superClassString" value="" />
</inspection_tool>
</profile>
</component>
\ No newline at end of file
<component name="libraryTable">
<component name="libraryTable">
<library name="Maven: 3rd.oracle:ojdbc:6">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/3rd/oracle/ojdbc/6/ojdbc-6.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/3rd/oracle/ojdbc/6/ojdbc-6-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/3rd/oracle/ojdbc/6/ojdbc-6-sources.jar!/" />
</SOURCES>
</library>
</component>
\ No newline at end of file
<component name="libraryTable">
<component name="libraryTable">
<library name="Maven: cn.hutool:hutool-all:4.5.16">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/cn/hutool/hutool-all/4.5.16/hutool-all-4.5.16.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/cn/hutool/hutool-all/4.5.16/hutool-all-4.5.16-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/cn/hutool/hutool-all/4.5.16/hutool-all-4.5.16-sources.jar!/" />
</SOURCES>
</library>
</component>
\ No newline at end of file
<component name="libraryTable">
<component name="libraryTable">
<library name="Maven: junit:junit:4.12">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12-sources.jar!/" />
</SOURCES>
</library>
</component>
\ No newline at end of file
<component name="libraryTable">
<component name="libraryTable">
<library name="Maven: org.hamcrest:hamcrest-core:1.3">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3-sources.jar!/" />
</SOURCES>
</library>
</component>
\ No newline at end of file
<component name="libraryTable">
<component name="libraryTable">
<library name="Maven: org.mybatis:mybatis:3.2.8">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/mybatis/mybatis/3.2.8/mybatis-3.2.8.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/org/mybatis/mybatis/3.2.8/mybatis-3.2.8-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/mybatis/mybatis/3.2.8/mybatis-3.2.8-sources.jar!/" />
</SOURCES>
</library>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="false" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/mybatisHomework.iml" filepath="$PROJECT_DIR$/mybatisHomework.iml" />
</modules>
</component>
</project>
\ No newline at end of file
This diff is collapsed.
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.mybatis:mybatis:3.2.8" level="project" />
<orderEntry type="library" name="Maven: 3rd.oracle:ojdbc:6" level="project" />
<orderEntry type="library" name="Maven: cn.hutool:hutool-all:4.5.16" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
</component>
</module>
\ No newline at end of file
<project xmlns="http://maven.apache.org/POM/4.0.0"
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cesgroup</groupId>
<artifactId>mybatisHomework</artifactId>
<name>mybatisHomework</name>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<dependency>
<groupId>3rd.oracle</groupId>
<artifactId>ojdbc</artifactId>
<version>6</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.5.16</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
package com.cesgroup.mybatisHomework.entity;
package com.cesgroup.mybatisHomework.entity;
import cn.hutool.json.JSONObject;
import java.io.Serializable;
public class Person implements Serializable {
private static final long serialVersionUID = -2109196136220379070L;
private String id;
private String name;
private JSONObject info;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public JSONObject getInfo() {
return info;
}
public void setInfo(JSONObject info) {
this.info = info;
}
}
package com.cesgroup.mybatisHomework.handle;
package com.cesgroup.mybatisHomework.handle;
import cn.hutool.json.JSONObject;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class PersonTypeHandle extends BaseTypeHandler<JSONObject> {
@Override
public JSONObject getNullableResult(ResultSet rs, String cloumName) throws SQLException {
String infoJsonString = rs.getString(cloumName);
JSONObject infObject = null;
try {
infObject = new JSONObject(infoJsonString);
//System.out.println("----将json串转成json对象成功!----");
} catch (Exception e) {
System.err.println("将json串转成json对象失败!");
e.printStackTrace();
}
return infObject;
}
@Override
public JSONObject getNullableResult(CallableStatement arg0, int arg1) throws SQLException {
return null;
}
@Override
public void setNonNullParameter(PreparedStatement statement, int i, JSONObject infoObject, JdbcType jdbcType) throws SQLException {
try {
String userInfoString = infoObject.toString();
statement.setString(i, userInfoString);
} catch (Exception e) {
System.out.println("----将对象转化为字符串失败!----");
e.printStackTrace();
}
}
@Override
public JSONObject getNullableResult(ResultSet arg0, int arg1) throws SQLException {
return null;
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<?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="MyMapper">
<select id="selectPerson" resultType="com.cesgroup.mybatisHomework.entity.Person">
select * from mybatis where id = #{id}
</select>
<insert id="insert" parameterType="com.cesgroup.mybatisHomework.entity.Person">
INSERT INTO mybatis (id, name, info) VALUES(#{id}, #{name}, #{info})
</insert>
<insert id="insertBatch">
INSERT INTO mybatis (id, name, info)
VALUES
<foreach collection="list" item="person" separator=",">
(#{person.id}, #{person.name}, #{person.info})
</foreach>
</insert>
</mapper>
\ No newline at end of file
driver=oracle.jdbc.driver.OracleDriver
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@192.168.146.128:1521:orcl
username=mybatis_homework
password=mybatis_homework
<?xml version="1.0" encoding="UTF-8" ?>
<?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="db.properties"></properties>
<typeHandlers>
<typeHandler handler="com.cesgroup.mybatisHomework.handle.PersonTypeHandle"></typeHandler>
</typeHandlers>
<!-- 环境,可以配置多个,default:指定采用哪个环境 -->
<environments default="dev">
<!-- id:唯一标识 -->
<environment id="dev">
<!-- 事务管理器,JDBC类型的事务管理器 -->
<transactionManager type="JDBC"/>
<!-- 数据源,池类型的数据源 -->
<dataSource type="POOLED">
<property name="driver" value="${driver}"/> <!-- 配置了properties,所以可以直接引用 -->
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="MyMapper.xml"/>
</mappers>
</configuration>
create table PERSON
create table PERSON
(
id VARCHAR2(50),
name VARCHAR2(50),
info VARCHAR2(2000)
)
package com;
package com;
import cn.hutool.core.io.IoUtil;
import cn.hutool.json.JSONObject;
import com.cesgroup.mybatisHomework.entity.Person;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class TestOne {
private SqlSessionFactory sqlSessionFactory = null;
private SqlSession sqlSession = null;
@Before
public void configMybatis() {
// 指定全局配置文件
String resource = "mybatis-config.xml";
// 读取配置文件
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
sqlSession = sqlSessionFactory.openSession(false);
} catch (Exception e) {
e.printStackTrace();
} finally {
IoUtil.close(inputStream);
}
}
/**
* TypeHandler查询
*/
@Test
public void selectTest() {
Person person = sqlSession.selectOne("MyMapper.selectPerson", 2);
System.out.println(person);
sqlSession.close();
}
/**
* TypeHandler新增
*/
@Test
public void insertTest() {
Person person = new Person();
person.setId("aaa");
person.setName("张三");
JSONObject userInfObject = new JSONObject();
userInfObject.put("sex", "男");
userInfObject.put("birthday", "1999-12-12");
person.setInfo(userInfObject);
int a = sqlSession.insert("MyMapper.insert", person);
sqlSession.commit();
System.out.println("影响的行数:" + a);
}
/**
* 批量插入1
*/
@Test
public void batchInsert1() {
long begin = System.currentTimeMillis();
List<Person> list = new ArrayList<>();
for (int i = 1; i < 10000; i++) {
Person person = new Person();
person.setId("a" + i);
person.setName("李四" + i);
JSONObject userInfObject = new JSONObject();
userInfObject.put("sex", "男");
userInfObject.put("birthday", "1999-12-12");
person.setInfo(userInfObject);
list.add(person);
}
sqlSession.insert("MyMapper.insertBatch", list);
sqlSession.commit();
long end = System.currentTimeMillis();
System.out.println("##############耗时:" + (end - begin) + " 毫秒");
}
/**
* 批量插入2
*/
@Test
public void batchInsert2() {
sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
long begin = System.currentTimeMillis();
for (int i = 1; i < 10000; i++) {
Person person = new Person();
person.setId("b" + i);
person.setName("王五" + i);
JSONObject userInfObject = new JSONObject();
userInfObject.put("sex", "男");
userInfObject.put("birthday", "1999-12-12");
person.setInfo(userInfObject);
int a = sqlSession.insert("MyMapper.insert", person);
}
sqlSession.commit();
long end = System.currentTimeMillis();
System.out.println("##############耗时:" + (end - begin) + " 毫秒");
}
}
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