最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Spring Boot+MyBatis框架做查詢操作的示例代碼

 更新時(shí)間:2018年10月18日 09:37:18   作者:cairui  
這篇文章主要介紹了使用Spring Boot+MyBatis框架做查詢操作的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧

一.在你建立的工程下創(chuàng)建 Module 選擇Spring initializr創(chuàng)建。

二.在Type處選擇: Maven Project(項(xiàng)目的構(gòu)建工具)

三.創(chuàng)建依賴時(shí)勾上web,mybatis,mysql(這個(gè)看你個(gè)人需要吧,可以自主選擇)

建立好的項(xiàng)目結(jié)構(gòu)如下:

注意:application.properties和application.yml是同一個(gè)東西,均為項(xiàng)目的核心配置文件

內(nèi)容如下:

#連接數(shù)據(jù)庫(kù)
spring.datasource.url=jdbc:mysql://localhost:3306/smbms
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.driverClassName=com.mysql.jdbc.Driver
#引入mybatis的配置文件
mybatis:
  mybatis.mapper-locations=classpath:mapper/*.xml
  mybatis.type-aliases-package=com.example.sprboot.pojo

相應(yīng)的pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example</groupId>
  <artifactId>springboot</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>springboot</name>
  <description>Demo project for Spring Boot</description>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <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>
    <!--添加web依賴-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--引入spring boot包-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    <!-- Spring-Mybatis -->
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>1.3.0</version>
    </dependency>
    <!-- MySQL -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <!--使用json對(duì)象-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.49</version>
    </dependency>
    <!--使用thymeleaf標(biāo)簽-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

相應(yīng)的接口UserMapper如下:

@Repository
public interface UserMapper {
  List<User> getList();
}

service如下:

public interface UserService {
  List<User> getList();
}

impl如下:

@Service
public class UserServiceImpl implements UserService {
  @Resource
  private UserMapper userMapper;
  @Override
  public List<User> getList() {
    return userMapper.getList();
  }
}

在resources中建一個(gè)文件夾mapper里面放mapper.xml文件,代碼如下:

<select id="getList" resultType="User">
  select * from smbms_user
</select>

在templates文件夾中建html文件(注意:Spring Boot中不能跳轉(zhuǎn)到.jsp文件,所以只能用html)

核心代碼如下:

<table>
  <th>工號(hào)</th>
  <th>用戶名</th>
  <th>姓名</th>
  <th>性別</th>
  <th>生日</th>
  <th>電話</th>
  <th>地址</th>
  <th>創(chuàng)建時(shí)間</th>
  <tr th:each="user : ${users}">
    <td th:text="${user.id}"></td>
    <td th:text="${user.userCode}"></td>
    <td th:text="${user.userName}"></td>
    <td th:text="${user.gender}"></td>
    <td th:text="${user.birthday}"></td>
    <td th:text="${user.phone}"></td>
    <td th:text="${user.address}"></td>
    <td th:text="${user.createdBY}"></td>
  </tr>
</table>

此處有一個(gè)th標(biāo)簽,需要引入一個(gè)<html xmlns:th="http://www.thymeleaf.org">

并在pom.xml中引入對(duì)應(yīng)的jar包(html中不能使用jstl表達(dá)式)

大家可以擴(kuò)展一下thymeleaf的知識(shí)

控制器代碼如下:

@Controller
public class UserController {
  @Resource
  private UserService userService;
@RequestMapping("/")
  public String getStuinforList(HttpServletRequest request, Model model){
    List<User> list=userService.getList();
    model.addAttribute("users",list);
    System.out.println(list);
    return "/index.html";
  }
}

注:在調(diào)通的時(shí)候,可能會(huì)報(bào)很多的錯(cuò),基本上都是注解的使用出錯(cuò),希望各位能夠細(xì)心點(diǎn)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java的Arrays工具類實(shí)戰(zhàn)

    java的Arrays工具類實(shí)戰(zhàn)

    java.util.Arrays類能方便地操作數(shù)組,它提供的所有方法都是靜態(tài)的。Arrays作為一個(gè)工具類,能很好的操作數(shù)組。下面介紹主要使用的幾個(gè)函數(shù)
    2016-12-12
  • 關(guān)于web項(xiàng)目讀取classpath下面文件的心得分享

    關(guān)于web項(xiàng)目讀取classpath下面文件的心得分享

    這篇文章主要介紹了關(guān)于web項(xiàng)目讀取classpath下面文件的心得,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Java如何定義Long類型

    Java如何定義Long類型

    這篇文章主要介紹了Java如何定義Long類型,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Java中字符串中連續(xù)相同字符去重方法

    Java中字符串中連續(xù)相同字符去重方法

    今天小編就為大家分享一篇Java中字符串中連續(xù)相同字符去重方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧
    2018-07-07
  • CompletableFuture?異步編排示例詳解

    CompletableFuture?異步編排示例詳解

    這篇文章主要為大家介紹了CompletableFuture?異步編排示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • Spring中@Autowired注解在不同方法的寫法示例

    Spring中@Autowired注解在不同方法的寫法示例

    這篇文章主要為大家介紹了Spring中@Autowired注解在不同方法的寫法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • springboot項(xiàng)目(jar包)指定配置文件啟動(dòng)圖文教程

    springboot項(xiàng)目(jar包)指定配置文件啟動(dòng)圖文教程

    這篇文章主要給大家介紹了關(guān)于springboot項(xiàng)目(jar包)指定配置文件啟動(dòng)的相關(guān)資料,在多環(huán)境部署過程中、及線上運(yùn)維中可能會(huì)遇到臨時(shí)指定配置文件的情況,需要的朋友可以參考下
    2023-07-07
  • springboot?通過博途獲取plc點(diǎn)位的數(shù)據(jù)代碼實(shí)現(xiàn)

    springboot?通過博途獲取plc點(diǎn)位的數(shù)據(jù)代碼實(shí)現(xiàn)

    這篇文章主要介紹了springboot?通過博途獲取plc點(diǎn)位的數(shù)據(jù)的代碼實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-08-08
  • SpringBoot處理JSON數(shù)據(jù)方法詳解

    SpringBoot處理JSON數(shù)據(jù)方法詳解

    這篇文章主要介紹了SpringBoot整合Web開發(fā)中Json數(shù)據(jù)處理,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-10-10
  • SpringBoot圖文并茂詳解如何引入mybatis與連接Mysql數(shù)據(jù)庫(kù)

    SpringBoot圖文并茂詳解如何引入mybatis與連接Mysql數(shù)據(jù)庫(kù)

    這篇文章主要介紹了SpringBoot如何引入mybatis與連接Mysql數(shù)據(jù)庫(kù),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07

最新評(píng)論

涿州市| 巴塘县| 宣威市| 梁平县| 精河县| 建昌县| 乌拉特前旗| 南宫市| 寿光市| 苍南县| 永嘉县| 乌兰县| 寻乌县| 镇赉县| 观塘区| 应城市| 囊谦县| 沈阳市| 十堰市| 水城县| 繁峙县| 广元市| 镇康县| 东山县| 苍溪县| 绥江县| 扶余县| 绥芬河市| 喀喇沁旗| 当涂县| 白玉县| 垫江县| 桑日县| 晋州市| 泊头市| 墨玉县| 四会市| 三门峡市| 渑池县| 柘城县| 应城市|