mysql test 映射到实体_从零搭建SpringBoot+MyBatis+MySQL工程
目錄
- 創(chuàng)建工程
- 了解MVC模型
- 屬性配置文件.yml
- springboot里的數(shù)據(jù)庫布局
- 創(chuàng)建包
- 創(chuàng)建數(shù)據(jù)表
- 創(chuàng)建實體類DAO(model)
- 創(chuàng)建mapper
- 語句直接寫在class中(全注解方式)
- 語句寫在xml中
- 添加掃描接口的注解
- controller
- 啟動項目
小白上路,寸草不生
若文章內(nèi)容有誤,歡迎留言指出~~~
創(chuàng)建工程
使用依賴:web(前后端交互)、mybatis(持久層)、mysql(數(shù)據(jù)庫驅(qū)動)
了解MVC模型
- 模型(model或server):一個或多個 JavaBean 對象,用于存儲數(shù)據(jù)(實體模型,由 JavaBean 類創(chuàng)建)和處理業(yè)務(wù)邏輯(業(yè)務(wù)模型,由一般的 Java 類創(chuàng)建)。
- 控制器(controller):一個或多個 Servlet 對象,根據(jù)視圖提交的請求進(jìn)行控制,即將請求轉(zhuǎn)發(fā)給處理業(yè)務(wù)邏輯的 JavaBean,并將處理結(jié)果存放到實體模型 JavaBean 中,輸出給視圖顯示。
- 視圖:一個或多個頁面,向控制器提交數(shù)據(jù)和為模型提供數(shù)據(jù)顯示,頁面主要使用 HTML 標(biāo)記和 JavaBean 標(biāo)記來顯示數(shù)據(jù)。
屬性配置文件.yml
使用更加方便的yml文件,而不是默認(rèn)的properties文件。刪除application.properties,新增application.yml和application-dev.yml,并修改內(nèi)容。
application.yml:
spring:profiles:active: devapplication-dev.yml:
server:port: 8080 spring:datasource:username: rootpassword: 123456driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC自動配置時會將數(shù)據(jù)庫連接相關(guān)信息注入到mybatis中.
springboot里的數(shù)據(jù)庫布局
- controller:類;接收http(get/post)請求,查詢數(shù)據(jù)庫,返回數(shù)據(jù)。
- mapper:接口;數(shù)據(jù)庫的各種查詢語句的定義。
- model:類;數(shù)據(jù)庫各字段的JavaBean,將數(shù)據(jù)庫映射為類。
創(chuàng)建包
創(chuàng)建數(shù)據(jù)表
CREATE TABLE `user` (`id` int(11) NOT NULL COMMENT 'id',`username` varchar(50) DEFAULT NULL,`password` varchar(100) DEFAULT NULL,PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;創(chuàng)建實體類DAO(model)
該類中跟數(shù)據(jù)表對應(yīng),提供get和set方法。
package com.sxf.demo.model; public class User {private Long id;private String name;private String pwd;public Long getId(){return id;}public String getName() {return name;}public String getPwd() {return pwd;}public void setId(Long id) {this.id = id;}public void setName(String name) {this.name = name;}public void setPwd(String pwd) {this.pwd = pwd;} }創(chuàng)建mapper
SQL的查詢語句。
語句直接寫在class中(全注解方式)
package com.sxf.demo.mapper; import com.sxf.demo.model.User; import org.apache.ibatis.annotations.Select; import java.util.List;public interface UserMapper {@Select("select * from user")List<User> getAllUsers();@Select("select * from user where id=#{id}")User getUserById(Long id); }語句寫在xml中
UserMapper.java:
package com.sxf.demo.mapper; import com.sxf.demo.model.User; import java.util.List;public interface UserMapper {User getUserById(Long id); }在resource文件夾下新建一個mapper文件夾編寫對應(yīng)的xml
UserMapper.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- 此處與接口類地址對應(yīng) --> <mapper namespace="com.sxf.demo.mapper.UserMapper"><!-- 此處與接口方法名對應(yīng) 指定參數(shù)類型與返回結(jié)果類型--><select id="getUserById" parameterType="java.lang.Long" resultType="com.sxf.demo.model.User">select * from user where id = #{id}</select> </mapper>注意:
1.namespace中需要與使用@Mapper的接口對應(yīng)
2.UserMapper.xml文件名稱必須與使用@Mapper的接口一致
3.標(biāo)簽中的id必須與@Mapper的接口中的方法名一致,且參數(shù)一致
在application-dev.yml中添加路徑,指明映射文件位置:
mybatis:mapper-locations: classpath:mapper/*Mapper.xmltype-aliases-package: com.sxf.demo.mapper注意:
1.mybatis中的mapper-locations是mapper的xml文件位置
2.mybatis中的type-aliases-package是為了配置xml文件中resultType返回值的包位置,如果未配置請使用全包名
添加掃描接口的注解
這里掃描的是接口,不是xml。
啟動類DemoApplication.java:
controller
邏輯代碼在這里吧。
package com.sxf.demo.controller; import com.sxf.demo.mapper.UserMapper; import com.sxf.demo.model.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.List;@Controller @ResponseBody public class UserController {@Autowired(required = false)private UserMapper userMapper;@RequestMapping("/getUser")public User getAllUsers(Long id){return userMapper.getUserById(id);} }啟動項目
瀏覽器訪問:
總結(jié)
以上是生活随笔為你收集整理的mysql test 映射到实体_从零搭建SpringBoot+MyBatis+MySQL工程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: shell 做加法运算_C语言探索之旅
- 下一篇: wamp 使用mysql_PHP当中如何