CREATE TABLE `tb_student` (
`student_id` bigint(20) NOT NULL,
`student_age` int(11) NULL DEFAULT NULL,
`student_gender` int(11) NULL DEFAULT NULL,
`student_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`student_id`) USING BTREE
);
-- ----------------------------
-- Records of tb_student
-- ----------------------------
INSERT INTO `tb_student` VALUES (2, 12, 2, '李四');
INSERT INTO `tb_student` VALUES (3, 11, 1, '安澜公子帅帅哒111');
plugins {
id 'org.springframework.boot' version '2.1.5.RELEASE'
id 'java'
id 'com.arenagod.gradle.MybatisGenerator' version '1.4'
}
apply plugin: 'io.spring.dependency-management'
group = 'org.virtue'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
//数据库连接使用druid连接池
compile('org.springframework.boot:spring-boot-starter-data-jpa') {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}
compile group: 'com.alibaba', name: 'druid', version: '1.0.29'
compile('mysql:mysql-connector-java:5.1.31')
// Mybatis 代码自动生成所引入的包
compile(group: 'org.mybatis.spring.boot', name: 'mybatis-spring-boot-starter', version: '1.3.2')
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
configurations {
mybatisGenerator
}
mybatisGenerator {
verbose = true
configFile = 'src/main/resources/generatorConfig.xml'
}
//mybatis generator plugin ------ end
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<!-- 配置生成器 -->
<generatorConfiguration>
<!--id:必选,上下文id,用于在生成错误时提示-->
<context id="mysql" targetRuntime="MyBatis3">
<!-- 生成的Java文件的编码 -->
<property name="javaFileEncoding" value="UTF-8"/>
<!-- 对注释进行控制 -->
<commentGenerator>
<!-- suppressDate是去掉生成日期那行注释 -->
<property name="suppressDate" value="true"/>
<!-- suppressAllComments是去掉所有的注解 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--jdbc的数据库连接 -->
<jdbcConnection
driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/microservice"
userId="root"
password="xda265856">
</jdbcConnection>
<!-- java类型处理器
用于处理DB中的类型到Java中的类型,默认使用JavaTypeResolverDefaultImpl;
注意一点,默认会先尝试使用Integer,Long,Short等来对应DECIMAL和 NUMERIC数据类型;
-->
<javaTypeResolver type="org.mybatis.generator.internal.types.JavaTypeResolverDefaultImpl">
<!--
true:使用BigDecimal对应DECIMAL和 NUMERIC数据类型
false:默认,
scale>0;length>18:使用BigDecimal;
scale=0;length[10,18]:使用Long;
scale=0;length[5,9]:使用Integer;
scale=0;length<5:使用Short;
-->
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- java模型创建器,是必须要的元素
负责:1,key类(见context的defaultModelType);2,java类;3,查询类
targetPackage:生成的类要放的包,真实的包受enableSubPackages属性控制;
targetProject:目标项目,指定一个存在的目录下,生成的内容会放到指定目录中,如果目录不存在,MBG不会自动建目录
-->
<javaModelGenerator targetPackage="org.virtue" targetProject="src/main/java">
<!-- 是否允许子包,即targetPackage.schemaName.tableName -->
<property name="enableSubPackages" value="true"/>
<!-- 是否对model添加 构造函数 -->
<property name="constructorBased" value="true"/>
<!-- 是否对类CHAR类型的列的数据进行trim操作 -->
<property name="trimStrings" value="true"/>
<!-- 建立的Model对象是否 不可改变 即生成的Model对象不会有 setter方法,只有构造方法 -->
<property name="immutable" value="false"/>
</javaModelGenerator>
<!-- 生成SQL map的XML文件生成器,
注意,在Mybatis3之后,我们可以使用mapper.xml文件+Mapper接口(或者不用mapper接口),
或者只使用Mapper接口+Annotation,
所以,如果 javaClientGenerator配置中配置了需要生成XML的话,这个元素就必须配置
targetPackage/targetProject:同javaModelGenerator
-->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<!-- 在targetPackage的基础上,根据数据库的schema再生成一层package,最终生成的类放在这个package下,默认为false -->
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 对于mybatis来说,即生成Mapper接口,注意,如果没有配置该元素,那么默认不会生成Mapper接口
targetPackage/targetProject:同javaModelGenerator
type:选择怎么生成mapper接口(在MyBatis3/MyBatis3Simple下):
1,ANNOTATEDMAPPER:会生成使用Mapper接口+Annotation的方式创建(SQL生成在annotation中),不会生成对应的XML;
2,MIXEDMAPPER:使用混合配置,会生成Mapper接口,并适当添加合适的Annotation,但是XML会生成在XML中;
3,XMLMAPPER:会生成Mapper接口,接口完全依赖XML;
注意,如果context是MyBatis3Simple:只支持ANNOTATEDMAPPER和XMLMAPPER
-->
<javaClientGenerator targetPackage="org.virtue.mapper" type="XMLMAPPER" targetProject="src/main/java">
<!-- 在targetPackage的基础上,根据数据库的schema再生成一层package,最终生成的类放在这个package下,默认为false -->
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 选择一个table来生成相关文件,可以有一个或多个table,必须要有table元素
tableName(必要):要生成对象的表名;
domainObjectName 给表对应的 model 起名字
注意:大小写敏感问题。
-->
<table tableName="tb_student" domainObjectName="pojo.Student">
<!--用来修改表中某个列的属性,一个table元素中可以有多个columnOverride元素哈.
property属性来指定列要生成的属性名称.
-->
<columnOverride column="student_id" property="studentId" />
<columnOverride column="student_age" property="studentAge" />
<columnOverride column="student_gender" property="studentGender" />
<columnOverride column="student_name" property="studentName" />
</table>
</context>
</generatorConfiguration>
server:
port: 8880
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://127.0.0.1:3306/microservice?useUnicode=true&characterEncoding=utf-8&useSSL=false
username : root
password : xda265856
driverClassName : com.mysql.jdbc.Driver
mybatis:
type-aliases-package: org.virtue.pojo
mapper-locations: classpath:mapper/*.xml
public interface IStudentService {
List<Student> findStudentByName(String name);
}
@Service
public class StudentServiceImpl implements IStudentService {
@Autowired
private StudentMapper studentMapper;
@Override
public List<Student> findStudentByName(String name) {
StudentExample studentExample = new StudentExample();
studentExample.createCriteria().andStudentNameEqualTo(name);
List<Student> students = studentMapper.selectByExample(studentExample);
return students;
}
}
@Controller
public class StudentController {
@Autowired
private StudentServiceImpl studentService;
@ResponseBody
@RequestMapping(value = "/students", method = {RequestMethod.GET})
public List<Student> students(String studentName){
List<Student> students = studentService.findStudentByName(studentName);
return students;
}
}
@SpringBootApplication
@MapperScan("org.virtue.mapper")
public class FastSpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(FastSpringbootApplication.class, args);
}
}
至此,mybastis集成结束。