还需要选中web模块
应用创建完成,现在查看pom文件中依赖:
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.3.0version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
查看mybatis-spring-boot-starter又依赖了那些:
查看上一篇章:SpringBoot学习之路(X2)- JDBC数据访问、自动配置原理、druid数据源配置
package com.yhhy.springboot.bean;
import java.util.Date;
public class Student {
private Integer id;
private String name;
private String pwd;
private String md5;
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getMd5() {
return md5;
}
public void setMd5(String md5) {
this.md5 = md5;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
package com.yhhy.springboot.controller;
import com.yhhy.springboot.bean.Student;
import com.yhhy.springboot.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StudentController {
//这里就不写实现类了
@Autowired
StudentMapper studentMapper;
//PathVariable 取值
@GetMapping("/student/{id}")
public Student getStudent(@PathVariable("id") Integer id){
return studentMapper.getStudentById(id);
}
@GetMapping("/student")
public Student insertStudent(Student student){
studentMapper.insertStudent(student);
return student;
}
}
这里的日期使用http参数传送有点问题,之后再修改
package com.yhhy.springboot.mapper;
import com.yhhy.springboot.bean.Student;
import org.apache.ibatis.annotations.*;
@Mapper
public interface StudentMapper {
/**
* 查询一个学生
* @param id
* @return
*/
@Select("select * from student where id = #{id}")
public Student getStudentById(Integer id);
/**
* 删除
* @param id
* @return 返回受影响的行数
*/
@Delete("delete from student where id=#{id}")
public Integer delStudentById(Integer id);
/**
* 添加
* @param student
* @return
*/
//使用自动生成的主键,并指定那个属性,用于添加记录后取主键id所用
@Options(useGeneratedKeys = true,keyProperty = "id")
@Insert("insert into student(name,pwd,md5,date) values(#{name},#{pwd},#{md5},#{date})")
public Integer insertStudent(Student student);
@Update("update student set name=#{name},pwd=#{pwd},md5=#{md5},date=#{date} where id = #{id}")
public Integer updaeStudent(Student student);
}
无论是注解版还是配置文件版,都需要将接口扫描到才行
package com.yhhy.springboot.mapper;
import com.yhhy.springboot.bean.Student;
//@Mapper或者@MapperScan将接口扫描装配到容器中
//这里的启动类已经标注了扫描
public interface Student02Mapper {
public Student getStudentById(Integer id);
public void insertStu(Student student);
}
这里在resources下创建:
快速配置mybatis中的配置文件:
到官方查看快速配置相关文档
<mapper namespace="com.yhhy.springboot.mapper.Student02Mapper">
<select id="getStudentById" resultType="com.yhhy.springboot.bean.Student">
SELECT * FROM student WHERE id=#{id}
select>
<insert id="insertStu">
INSERT INTO student(stuName,pwd,md5) VALUES (#{stuName},#{pwd},#{md5})
insert>
mapper>
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
settings>
configuration>
mybatis:
config-location: classpath:mybatis/mybatis-config.xml #指定全局配置文件的位置
mapper-locations: classpath:mybatis/mapper/*.xml #指定sql映射文件的位置
package com.yhhy.springboot.controller;
import com.yhhy.springboot.bean.Student;
import com.yhhy.springboot.mapper.Student02Mapper;
import com.yhhy.springboot.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StudentController {
@Autowired
Student02Mapper student02Mapper;
/**
* 查询一个学生
* PathVariable 获取传送的id值
*/
@GetMapping("/stu/{id}")
public Student getStu(@PathVariable("id") Integer id){
return student02Mapper.getStudentById(id);
}
}
数据库中的字段为stu_name,而实体类为stuName,是因为我们在mybatis-config.xml中配置了:mapUnderscoreToCamelCase
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
settings>
也可以查看官方文档中的配置项设置:
http://www.mybatis.org/mybatis-3/configuration.html#settings
可以看到注解版和配置文件版是可以混合使用的:
在启动类上添加扫描:
@MapperScan(value = “com.yhhy.springboot.mapper”)
注释掉mapper上的注解@Mapper,测试结果依然可以;
例如:
在mysql中的字段为stu_name ,而实体类中的属性为stuName,怎么才能映射上?
package com.yhhy.springboot.config;
import org.apache.ibatis.session.Configuration;
import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;
import org.springframework.context.annotation.Bean;
//此注解使用的是全类名,因为上面已经导入了org.apache.ibatis.session.Configuration
@org.springframework.context.annotation.Configuration
public class MyBatisConfig {
@Bean
public ConfigurationCustomizer configurationCustomizer(){
return new ConfigurationCustomizer() {
@Override
public void customize(Configuration configuration) {
//自定义的驼峰命名规则
configuration.setMapUnderscoreToCamelCase(true);
}
};
}
}
可以映射上;