StringBoot 整合Mybatis
阿新 • • 發佈:2018-12-20
1.SpringBoot 簡介
1.1 Spring Team在現有Spring框架的基礎上釋出了一個創新的框架:Spring Boot。 1.2 Spring Boot的開發團隊是:PivotalSpring Boot的主要作用是:簡化開發,減少配置(簡化配置和部署spring應用程式的過程)。 1.3 Spring Boot框架提倡:一鍵部署、習慣優於配置Spring Boot還是一個微框架,它與目1.4 前流行的微服務緊密聯絡,可以開發微應用。
2.引入依賴
<!-- 引入Mybatis依賴 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <!-- 引入Mysql依賴 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.44</version> </dependency> <!-- 匯入資料來源C3p0 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.1</version> </dependency> <!-- spring Mybatis 整合包 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency>
3.配置資料來源,和載入Mybatis配置
3.1.在application.properties中配置資料來源
#配置資料來源 spring.datasource.url=jdbc:mysql://localhost:3306/llmjsh spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.username=root spring.datasource.password=root # 載入mybatis 配置 mybatis.mapper-locations=classpath:mapper/*.xml mybatis.config-location=classpath:mybatis/mybatis-config.xml
比如這樣!!!
4.在 src/main/resources 下建立 mybatis 資料夾,並在 mybatis 資料夾中建立 “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="useGeneratedKeys" value="true"/> <!-- 使用列別名替換列名,預設為 true --> <setting name="useColumnLabel" value="true"/> <!-- 開啟駝峰命名轉換:Table(create_time) => Entity(createTime) --> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> </configuration>
5.建立資料庫
5.建立實體類
此處省略get,set
public class Student implements Serializable {
private int uid;
private String name;
private String sex;
private String age;
private String address;
}
6.建立dao介面
public interface Studentdao {
Student studentAll();
}
7.建立Service
public interface StudentService { Student studentAll();
}
8.建立ServiceImpl 實現類
@Service("studentService")
public class StudentServiceImpl implements StudentService{
@Resource
private Studentdao studentdao;
@Override
public Student studentAll() {
return studentdao.studentAll();
}
}
7.建立StudentMapper.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.llmjsh.springboot1.dao.Studentdao">
<select id="studentAll" resultType="com.llmjsh.springboot1.pojo.Student">
select * from tb_Student
</select>
</mapper>
8.在controller中呼叫
@Controller
public class StudentController {
@Resource
private StudentServiceImpl studentService;
@RequestMapping(value = "/hello",produces="text/html;charset=UTF-8")
@ResponseBody
public Student Test(){
Student students = studentService.studentAll();
System.out.println(students);
return students;
}
}