1.springboot整合mybatis
1.在pom.xml文件中导入依赖坐标
<!-- 加载mybatis整合springboot -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!-- MySQL的jdbc驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
2.application.properties中配置MyBatis的Mapper.xml文件所在位置以及数据源
mybatis.mapper-locations=classpath:com/example/demo/mapper/*.xml
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&characterEncoding=utf8
3.主启动类上添加mybatis扫描包注解
@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
2.利用mybatis-generator生成代码
1.在pom.xml添加插件
<!--mybatis代码自动生成插件-->
</build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<!--配置文件的位置-->
<configurationFile>src/main/resources/GeneratorMapper.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
2.在src/main/resources/生成GeneratorMapper.xml配置文件
<?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>
<!-- 指定连接数据库的JDBC驱动包所在位置,指定到你本机的完整路径 -->
<classPathEntry location="D:\jar\jdbc.jar\mysql-connector-java-8.0.17.jar"/>
<!-- 配置table表信息内容体,targetRuntime指定采用MyBatis3的版本 -->
<context id="tables" targetRuntime="MyBatis3">
<!-- 抑制生成注释,由于生成的注释都是英文的,可以不让它生成 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 配置数据库连接信息 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/test?serverTimezone=UTC&characterEncoding=utf8"
userId="root"
password="123456">
</jdbcConnection>
<!-- 生成model类,targetPackage指定model类的包名, targetProject指定生成的model放在eclipse的哪个工程下面-->
<javaModelGenerator targetPackage="com.example.demo.model" targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
<property name="trimStrings" value="false" />
</javaModelGenerator>
<!-- 生成MyBatis的Mapper.xml文件,targetPackage指定mapper.xml文件的包名, targetProject指定生成的mapper.xml放在eclipse的哪个工程下面 -->
<sqlMapGenerator targetPackage="com.example.demo.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- 生成MyBatis的Mapper接口类文件,targetPackage指定Mapper接口类的包名, targetProject指定生成的Mapper接口放在eclipse的哪个工程下面 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 数据库表名及对应的Java模型类名 注意这里
需要表有主键才能生成主键的相关查询删除修改 -->
<table tableName="student"
domainObjectName="Student"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"
enableSelectByPrimaryKey="true"
enableUpdateByPrimaryKey="true"
enableDeleteByPrimaryKey="true" />
</context>
</generatorConfiguration>
配置好后点击右侧的maven项目框在双击所选中的插件即可生成代码
3.测试连接mybatis
方便起见这里
1.在mapper包下StudentMapper中添加了Student findStudentByName(String name)方法通过学生姓名查找学生的全部信息
2.在mapper包下StudentMapper.xml中编写查询sql语句
3.在service包下编写StudentService接口并编写其实现类
4.在controllerbao包下编写控制器
5.输入指定路径返回查询结果
表的名称是student
结构是name varchar(20),address varchar(20)
StudentMapper
public interface StudentMapper {
int insert(Student record);
int insertSelective(Student record);
Student findStudentByName(String name);
}
StudentMapper.xml
<select id="findStudentByName" parameterType="String" resultMap="BaseResultMap">
select *
from student
where name = #{name}
</select>
StudentService
public interface StudentService {
public Student find();
}
StudentImpl
@Service
public class StudentImpl implements StudentService {
@Autowired
private StudentMapper studentMapper;
@Override
public Student find() {
return studentMapper.findStudentByName("123");
}
}
MybatisController
@RestController
public class MybatisController {
@Autowired
private StudentService studentService;
@GetMapping("/find")
public Object students() {
return studentService.find();
}
}