使用的是阿里的驱动包,数据库是mysql
首先是pom文件:
4.0.0
com.springboot
springboot
0.0.1-SNAPSHOT
jar
springboot
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.3.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
com.alibaba
druid
1.0.25
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.1
org.springframework.boot
spring-boot-maven-plugin
然后是配置文件:application.yml 这个文件是自己创建的
spring:
datasource:
url: ****
driverClassName: *****
username: ****
password: ****
type: com.alibaba.druid.pool.DruidDataSource
mybatis:
type-aliases-package: com.springboot.springboot.model
mapper-locations: classpath:*/*.xml
springboot的入口类:
package com.springboot.springboot;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.springboot.springboot.dao")
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
model 实体类:
package com.springboot.springboot.model;
/**
* @Title: Student
* @Package com.springboot.springboot.model
* * @author 邓玉龙
* @Description:
* @date 2018-06-15 11:05
*/
public class Student {
private int id;
private String name;
private int age;
private String sex;
public Student(){}
public Student(int id, String name, int age, String sex) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
'}';
}
}
Mapper.xml文件:
insert into Student(ID,Name,Age,Sex)values (
#{id,jdbcType=INTEGER},#{name,jdbcType=VARCHAR},
#{age,jdbcType=INTEGER},#{sex,jdbcType=VARCHAR})
Dao 的接口
package com.springboot.springboot.dao;
import com.springboot.springboot.model.Student;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
/**
* @Title: StudentMapper
* @Package com.springboot.springboot.dao
* * @author 邓玉龙
* @Description:
* @date 2018-06-15 11:16
*/
public interface StudentMapper {
void saveStudent(Student student);
}
Service实现类和接口:
package com.springboot.springboot.service;
import com.springboot.springboot.model.Student;
/**
* @Title: StudentService
* @Package com.springboot.springboot.service
* * @author 邓玉龙
* @Description:
* @date 2018-06-15 11:31
*/
public interface StudentService {
void saveStudent(Student student);
}
package com.springboot.springboot.service;
import com.springboot.springboot.dao.StudentMapper;
import com.springboot.springboot.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
/**
* @Title: StudentServiceImpl
* @Package com.springboot.springboot.service
* * @author 邓玉龙
* @Description:
* @date 2018-06-15 11:31
*/
@Transactional
@Service
public class StudentServiceImpl implements StudentService{
@Resource
private StudentMapper studentMapper;
public void saveStudent(Student student){
System.out.println("保存数据开始");
student.setId(1);
student.setName("小美眉");
student.setAge(18);
student.setSex("36E");
studentMapper.saveStudent(student);
}
}
最后是controller层:
package com.springboot.springboot.controller;
import com.springboot.springboot.model.Student;
import com.springboot.springboot.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Title: StudentController
* @Package com.springboot.springboot.controller
* * @author 邓玉龙
* @Description:
* @date 2018-06-15 15:08
*/
@RestController
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping(value="/save")
public void sTest(){
studentService.saveStudent(new Student());
}
}
最后启动入口类然后访问这个方法: http://localhost:8080/save
https://blog.csdn.net/qq_38128218/article/details/80706447