项目目录:
说明,本文中有两个mapper接口,分别采用xml形式和java注解形式实现。
1、项目创建完成后,pom.xml文件配置
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
<dependencies>
<!-- springboot 整合web组件 整合springmvc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.29</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
</dependency>
</dependencies>
2、application.yml配置
spring:
datasource:
# 数据源基本配置
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/demo
type: com.alibaba.druid.pool.DruidDataSource
server:
port: 8888
mybatis:
#在yml文件中做了以下配置就无需做mybatis-config.xml配置,如果使用了mybatis-config.xml配置,就把下面两行注掉,将第三行配置打开
mapper-locations: classpath:mybatis/mappers/*.xml
type-aliases-package: com.example.mybatis.entity
#config-location: classpath:mybatis-config.xml
3、在 resources 下创建个 mybatis-config.xml 内容如下:
<?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>
<!-- 使用全局的映射器启用或者禁用缓存。 -->
<setting name="cacheEnabled" value="true"/>
<!-- 这是默认的执行类型 (SIMPLE: 简单; REUSE: 执行器可能重复使用prepared statements语句;BATCH: 执行器可以重复执行语句和批量更新) -->
<setting name="defaultExecutorType" value="SIMPLE"/>
<!-- 设置驱动等待数据响应的超时数 默认没有设置-->
<setting name="defaultStatementTimeout" value="30000"/>
<!-- 使用驼峰命名法转换字段。 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- 允许JDBC 生成主键。需要驱动器支持。如果设为了true,这个设置将强制使用被生成的主键,有一些驱动器不兼容不过仍然可以执行。 default:false -->
<setting name="useGeneratedKeys" value="true"/>
</settings>
<!--别名 配置别名后不区分大小写-->
<typeAliases>
<typeAlias alias="person" type="com.example.mybatis.entity.Person"/>
</typeAliases>
<!--映射器-->
<mappers>
<mapper resource="mappers/PersonMapper.xml"/>
</mappers>
</configuration>
4、在entity中创建实体类Deptment和Person类
Person类
@Data
public class Person {
private int id;
private String PersonName;
private int department_id;
}
Deptment类
@Data
public class Department {
private int id;
private String name;
private int testId;
private String data1;
private String data2;
private String data3;
}
5、创建Mapper,Mapper类和Mapper对应的xml,注册Mapper接口要增加注解@Mapper 如下:(Mapper接口 如果不加 @Mapper 则需要在启动类上增加 @MapperScan("mapper接口所在包名") )
其中DeptMapper接口,采用注解实现
@Mapper
public interface DeptMapper {
@Select("select * from department where id=#{id}")
Department getDepById(int id);
@Delete("delete from department where id=#{id}")
int deleteDepById(int id);
@Options(useGeneratedKeys = true,keyProperty = "id")
@Insert("insert into department(name) values(#{name})")
int insertDept(Department department);
@Update("update department set name=#{name} where id=#{id}")
int updateDept(Department department);
}
PersonMapper接口采用xml实现,
@Mapper
public interface PersonMapper {
Person getPersonById(int id);
String insertPerson(Person person);
}
PersonMapper.xml
<?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.example.mybatis.mapper.PersonMapper">
<select id="getPersonById" parameterType="int" resultType="person">
select * from person where id = #{id}
</select>
<insert id="insertPerson">
insert into person(PersonName,department_id) values (#{PersonName},#{department_id})
</insert>
<update id="updatePerson">
</update>
</mapper>
6、创建Controller 定义访问接口,如下:
PersonController
@RestController
public class PersonController {
@Autowired
PersonMapper personMapper;
@GetMapping("/per/{id}")
public Person getPersonById(@PathVariable int id){
return personMapper.getPersonById(id);
}
}
DepController
@RestController
public class DepController {
@Autowired
DeptMapper deptMapper;
@GetMapping("/dept/{id}")
public Department getDeptMapper(@PathVariable int id) {
return deptMapper.getDepById(id);
}
@GetMapping("/dept")
public Department insertDeptMapper( Department department){
System.out.println("准备进行插入数据操作!");
deptMapper.insertDept(department);
return department;
}
}
6、定义启动类,注意此类需要多增加一个注解@MapperScan("mapper类所在包名"),如下:(如果启动类增加@MapperSscan 那么 Mapper接口上则可不用加 @Mapper)
@MapperScan(value = "com.example.mybatis.mapper")
@SpringBootApplication
public class MybatisApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisApplication.class, args);
System.out.println("启动成功......");
}
}
8、运行正常