pom依赖
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
在application.properties文件中加入
spring.datasource.url=jdbc:mysql://localhost:3306/exam?useSSL=false
spring.datasource.username=root
spring.datasource.password=admin
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
创建实体类 Student.java
public class Student {
private int sid;
private String sname;
private int age;
private String gander;
private String province;
private String tuition;
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGander() {
return gander;
}
public void setGander(String gander) {
this.gander = gander;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getTuition() {
return tuition;
}
public void setTuition(String tuition) {
this.tuition = tuition;
}
@Override
public String toString() {
return "Student{" +
"sid=" + sid +
", sname='" + sname + '\'' +
", age=" + age +
", gander='" + gander + '\'' +
", province='" + province + '\'' +
", tuition='" + tuition + '\'' +
'}';
}
}
创建Mapper
import com.demo.test.entity.Student;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface StudentMapper {
@Select("select * from stu where sname = #{name} ")
Student findByName(@Param("name") String name);
@Select("select * from stu where age = #{age} ")
List<Student> findByAge(@Param("age") String age);
}
创建Service
import com.demo.test.mapper.StudentMapper;
import com.demo.test.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentService {
@Autowired
private StudentMapper studentMapper;
public Student findByName(String name){
return studentMapper.findByName(name);
}
public List<Student> findByAge(String age){
return studentMapper.findByAge(age);
}
}
创建运行类 (最好把该文件放在项目跟目录下)
import com.demo.test.Service.StudentService;
import com.demo.test.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class Test implements CommandLineRunner{
@Autowired
private StudentService studentService;
@Override
public void run(String... args) throws Exception {
Student name = studentService.findByName("张娜");
System.out.println(name.toString());
List<Student> byAge = studentService.findByAge("23");
for (Student student : byAge) {
System.out.println(student.toString());
}
}
}
创建启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}