Commit 2d51ee4e authored by zhouxi's avatar zhouxi

Merge remote-tracking branch 'origin/master'

parents b11e7ee1 664e64a1
++ "b/\345\274\240\345\277\227\345\242\231_Mybatis1/.gitkeep"
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.mybatis</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mybatis</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.2.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.59</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1.项目resource下的application.yml为配置文件,请修改datasource下的url、username、password
1.项目resource下的application.yml为配置文件,请修改datasource下的url、username、password
创建对应数据表 usertest :
/*Table structure for table `usertest` */
DROP TABLE IF EXISTS `usertest`;
CREATE TABLE `usertest` (
`name` varchar(50) COLLATE utf8_bin DEFAULT NULL COMMENT '姓名',
`sex` varchar(50) COLLATE utf8_bin DEFAULT NULL COMMENT '性别',
`hobby` varchar(1000) COLLATE utf8_bin DEFAULT NULL COMMENT '爱好',
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='用户信息表';
2.打开项目执行测试用例
\ No newline at end of file
++ "b/\345\274\240\345\277\227\345\242\231_Mybatis1/src/.gitkeep"
++ "b/\345\274\240\345\277\227\345\242\231_Mybatis1/src/main/.gitkeep"
++ "b/\345\274\240\345\277\227\345\242\231_Mybatis1/src/main/java/.gitkeep"
++ "b/\345\274\240\345\277\227\345\242\231_Mybatis1/src/main/java/com/.gitkeep"
++ "b/\345\274\240\345\277\227\345\242\231_Mybatis1/src/main/java/com/mybatis/.gitkeep"
package com.mybatis;
package com.mybatis;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MybatisApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisApplication.class, args);
}
}
++ "b/\345\274\240\345\277\227\345\242\231_Mybatis1/src/main/java/com/mybatis/handle/.gitkeep"
package com.mybatis.handle;
package com.mybatis.handle;
import com.mybatis.model.Context;
import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@MappedTypes(Context.class)
@MappedJdbcTypes(JdbcType.VARCHAR)
public class ContentTypeHandler extends BaseTypeHandler<Context> {
@Override
public void setNonNullParameter(PreparedStatement preparedStatement, int i, Context context, JdbcType jdbcType) throws SQLException {
String str = JSONObject.toJSONString(context);
preparedStatement.setString(i,str);
}
@Override
public Context getNullableResult(ResultSet resultSet, String s) throws SQLException {
String string = resultSet.getString(s);
return JSONObject.parseObject(string, Context.class);
}
@Override
public Context getNullableResult(ResultSet resultSet, int i) throws SQLException {
String string = resultSet.getString(i);
return JSONObject.parseObject(string, Context.class);
}
@Override
public Context getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
String string = callableStatement.getString(i);
return JSONObject.parseObject(string, Context.class);
}
}
++ "b/\345\274\240\345\277\227\345\242\231_Mybatis1/src/main/java/com/mybatis/mapper/.gitkeep"
package com.mybatis.mapper;
package com.mybatis.mapper;
import com.mybatis.handle.ContentTypeHandler;
import com.mybatis.model.User;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface UserMapper {
@Insert("INSERT INTO USERTEST(NAME,SEX,HOBBY) VALUES (#{name},#{sex}," +
"#{hobby,typeHandler=com.mybatis.handle.ContentTypeHandler})")
int insert(User user);
@Insert({
"<script>",
"insert into USERTEST(name, sex, hobby) values ",
"<foreach collection='list' item='item' separator=','>",
"(#{item.name}, #{item.sex}, #{item.hobby,typeHandler=com.mybatis.handle.ContentTypeHandler})",
"</foreach>",
"</script>"
})
boolean batchInsert(@Param(value="list")List<User> list);
}
++ "b/\345\274\240\345\277\227\345\242\231_Mybatis1/src/main/java/com/mybatis/model/.gitkeep"
package com.mybatis.model;
package com.mybatis.model;
public class Context {
String username;
String love;
public Context(){}
public Context(String name,String msg){
this.username=name;
this.love=msg;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getLove() {
return love;
}
public void setLove(String love) {
this.love = love;
}
}
package com.mybatis.model;
package com.mybatis.model;
public class User {
String name;
String sex;
Context hobby;
public User(String name,String sex,Context hobby){
this.name=name;
this.sex=sex;
this.hobby=hobby;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Context getHobby() {
return hobby;
}
public void setHobby(Context hobby) {
this.hobby = hobby;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", sex=" + sex +
", hobby=" + hobby +
'}';
}
}
++ "b/\345\274\240\345\277\227\345\242\231_Mybatis1/src/main/resources/.gitkeep"
spring:
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/ces?useUnicode=true&characterEncoding=utf8&autoReconnect=true&serverTimezone=Asia/Shanghai
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 8
MinIdle: 5
MaxActive: 20
MaxWait: 6000
timeBetweenEvictionRumsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatement: true
filters: stat,wall,logback
maxPoolPreparedStatementPerCommectionSize: 25
useGlobalDataSourceStat: true
commectionProperties: druid.stat.mergeSql=true,druid.stat.slowSqlMillis=500
mybatis:
configuration:
map-underscore-to-camel-case: true
lazy-loading-enabled: false
auto-mapping-behavior: full
useGeneratedKeys: true
useColumnLabel: true
mapUnderscoreToCamelCase: true
++ "b/\345\274\240\345\277\227\345\242\231_Mybatis1/src/test/.gitkeep"
++ "b/\345\274\240\345\277\227\345\242\231_Mybatis1/src/test/java/.gitkeep"
++ "b/\345\274\240\345\277\227\345\242\231_Mybatis1/src/test/java/com/.gitkeep"
++ "b/\345\274\240\345\277\227\345\242\231_Mybatis1/src/test/java/com/mybatis/.gitkeep"
package com.mybatis;
package com.mybatis;
import com.mybatis.mapper.UserMapper;
import com.mybatis.model.Context;
import com.mybatis.model.User;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.TransactionIsolationLevel;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
class MybatisApplicationTests {
private static final AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext("application.yml");
@Autowired
DataSource dataSource;
@Autowired
UserMapper userMapper;
@Test
void contextLoads() throws SQLException {
Context context = new Context("123","阿萨德");
User user = new User("test","男",context);
userMapper.insert(user);
}
@Test
void batchInsert(){
List<User> users = new ArrayList<User>();
for(int i=0;i<2;i++){
Context context = new Context("context"+i,"lowww"+i);
User user = new User("名字"+i,"男"+i,context);
users.add(user);
}
boolean isSuccess = userMapper.batchInsert(users);
}
}
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