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

Spring Boot基礎入門之基于注解的Mybatis

 更新時間:2018年07月11日 09:14:42   作者:小崔的筆記本  
這篇文章主要給大家介紹了關于Spring Boot基礎入門之基于注解的Mybatis的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

前言

今天學習下SpringBoot集成mybatis,集成mybatis一般有兩種方式,一個是基于注解的一個是基于xml配置的。今天先了解下基于注解的mybatis集成。下面話不多說了,來一起看看詳細的介紹吧

一、引入依賴項

因為是mybatis嘛,肯定是要有mybatis相關的,同時用的是mysql,所以也需要引入mysql相關的。

<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
 <dependency>
 <groupId>org.mybatis.spring.boot</groupId>
 <artifactId>mybatis-spring-boot-starter</artifactId>
 <version>1.3.2</version>
 </dependency>
 <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
 <dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
 <version>8.0.11</version>
 </dependency>

二、創(chuàng)建model

這里創(chuàng)建了一個User的model,這樣方便與數(shù)據(jù)庫的表對照,這里在mysql中創(chuàng)建了一個名為mybatis的數(shù)據(jù)庫,里面創(chuàng)建了一個user的表.同時創(chuàng)建了枚舉類UserSexEnum.

CREATE TABLE `user` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(20) DEFAULT NULL,
 `age` int(11) DEFAULT NULL,
 `sex` varchar(20) DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
package com.example.model;

import java.io.Serializable;

public class User implements Serializable{
 @Override
 public String toString() {
 // TODO Auto-generated method stub
 return "User [id=" + Id + ", name=" + Name + ", age=" + Age + "]";

 }

 public int getId() {
 return Id;
 }
 public void setId(int id) {
 Id = id;
 }
 public String getName() {
 return Name;
 }
 public void setName(String name) {
 Name = name;
 }
 public int getAge() {
 return Age;
 }
 public void setAge(int age) {
 Age = age;
 }
 private int Id;
 private String Name;
 private int Age; 
 
 private UserSexEnum Sex;

 public UserSexEnum getSex() {
 return Sex;
 }
 public void setSex(UserSexEnum sex) {
 Sex = sex;
 }
}
package com.example.model;

public enum UserSexEnum {
 MAN, WOMAN
}

三、創(chuàng)建Mapper

這里需要把model與操作數(shù)據(jù)庫的sql對照起來,用什么對照呢?那就需要創(chuàng)建一個mapper.這里有增刪改查。

package com.example.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.example.model.*;;
public interface UserMapper {
 @Select("SELECT * FROM user")
 @Results({
 @Result(property = "Sex", column = "sex", javaType = UserSexEnum.class),
 @Result(property = "Name", column = "name")

 })

 List<User> getAll();
 @Select("SELECT * FROM user WHERE id = #{id}")

 @Results({
 @Result(property = "Sex", column = "sex", javaType = UserSexEnum.class),
 @Result(property = "Name", column = "name")
 })

 User getOne(int id);
 @Insert("INSERT INTO user(name,age,sex) VALUES(#{name}, #{age}, #{sex})")
 void insert(User user);
 @Update("UPDATE user SET name=#{userName},age=#{age} WHERE id =#{id}")
 void update(User user);
 @Delete("DELETE FROM user WHERE id =#{id}")
 void delete(int id);
}

四、配置掃描

上面配置了mapper,那怎么讓系統(tǒng)知道m(xù)apper放在哪里呢?于是有了@MapperScan注解。

package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.example.mapper")
public class DemoApplication {

 public static void main(String[] args) {
 SpringApplication.run(DemoApplication.class, args);
 }
}

五、創(chuàng)建Controller

這里創(chuàng)建了UserController,一個是顯示所有用戶,一個是新增一個用戶之后再顯示所有用戶。

package com.example.demo;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.example.mapper.UserMapper;
import com.example.model.User;
import com.example.model.UserSexEnum;

@Controller
@RequestMapping("/user")
public class UserController {
 
 @Autowired
 private UserMapper userMapper;
 
 @RequestMapping(value = "/alluser.do",method = RequestMethod.GET)
 public String getallusers(Model model) {
 List<User> users=userMapper.getAll();
 model.addAttribute("users", users);
 return "userlist";
 }
 @RequestMapping(value = "/insert.do",method = RequestMethod.GET)
 public String adduser(Model model) {
 User user=new User();
 user.setName("cuiyw");
 user.setAge(27);
 user.setSex(UserSexEnum.MAN);
  
 userMapper.insert(user);
 List<User> users=userMapper.getAll();
 model.addAttribute("users", users);
 return "userlist";
 }
}

六、數(shù)據(jù)庫配置

上面mapper也設置了,model也設置了,那要與數(shù)據(jù)庫交互,肯定要配置數(shù)據(jù)庫地址這些信息吧。這里在運行的時候還報了一個錯誤.nested exception is java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.在mysql中設置了下時區(qū):set global time_zone='+8:00';

spring.mvc.view.prefix=/view/

spring.mvc.view.suffix=.jsp
mybatis.type-aliases-package=com.example.model

spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/mybatis
spring.datasource.username = root
spring.datasource.password = 123456

七、創(chuàng)建頁面顯示

這里還是按照上一博客用jsp顯示數(shù)據(jù)。

<%@ page language="java" contentType="text/html; charset=utf-8"
 pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
 <table>
 <tr><th>名字</th><th>年齡</th><th>性別</th></tr>
 <c:forEach items="${users}" var="item">
  <tr><td>${item.name}</td><td>${item.age}</td><td>${item.sex}</td></tr>
 </c:forEach>
 </table>
</body>
</html>

八、測試

這里先在瀏覽器打開http://localhost:8080/user/alluser.do,可以看到用戶列表,然后輸入http://localhost:8080/user/insert.do,就會看到列表顯示多了一行數(shù)據(jù)。

九、小結

使用基于注解的集成mybatis比較省事方便,但有利有弊,對于多表相連的可能就不太方便,使用基于xml配置的可能就更會好些。

好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關文章

  • My eclipse 端口占用(9360)問題解決辦法

    My eclipse 端口占用(9360)問題解決辦法

    這篇文章主要介紹了My eclipse 工程發(fā)布時出現(xiàn)端口占用問題解決辦法的相關資料,需要的朋友可以參考下
    2016-12-12
  • SpringCloud服務接口調(diào)用OpenFeign及使用詳解

    SpringCloud服務接口調(diào)用OpenFeign及使用詳解

    這篇文章主要介紹了SpringCloud服務接口調(diào)用——OpenFeign,在學習Ribbon時,服務間調(diào)用使用的是RestTemplate+Ribbon實現(xiàn),而Feign在此基礎上繼續(xù)進行了封裝,使服務間調(diào)用變得更加方便,需要的朋友可以參考下
    2023-04-04
  • SpringBoot登錄驗證碼實現(xiàn)過程詳解

    SpringBoot登錄驗證碼實現(xiàn)過程詳解

    這篇文章主要介紹了SpringBoot登錄驗證碼實現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-04-04
  • Java WebService 簡單實例(附實例代碼)

    Java WebService 簡單實例(附實例代碼)

    本篇文章主要介紹了Java WebService 簡單實例(附實例代碼), Web Service 是一種新的web應用程序分支,他們是自包含、自描述、模塊化的應用,可以發(fā)布、定位、通過web調(diào)用。有興趣的可以了解一下
    2017-01-01
  • Spring?Security配置多個數(shù)據(jù)源并添加登錄驗證碼的實例代碼

    Spring?Security配置多個數(shù)據(jù)源并添加登錄驗證碼的實例代碼

    這篇文章主要介紹了Spring?Security配置多個數(shù)據(jù)源并添加登錄驗證碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-08-08
  • Java業(yè)務校驗工具實現(xiàn)方法

    Java業(yè)務校驗工具實現(xiàn)方法

    這篇文章主要介紹了Java業(yè)務校驗工具實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-06-06
  • JAVA正則表達式及字符串的替換與分解相關知識總結

    JAVA正則表達式及字符串的替換與分解相關知識總結

    今天給大家?guī)淼氖顷P于Java的相關知識總結,文章圍繞著JAVA正則表達式及字符串的替換與分解展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • Java使用正則表達式判斷字符串是否以字符開始

    Java使用正則表達式判斷字符串是否以字符開始

    這篇文章主要介紹了Java使用正則表達式判斷字符串是否以字符開始的相關資料,需要的朋友可以參考下
    2017-06-06
  • 消息中間件ActiveMQ的簡單入門介紹與使用

    消息中間件ActiveMQ的簡單入門介紹與使用

    消息隊列是指利用高效可靠的消息傳遞機制進行與平臺無關的數(shù)據(jù)交流,并基于數(shù)據(jù)通信來進行分布式系統(tǒng)的集成,這篇文章主要給大家介紹了關于ActiveMQ的簡單入門介與使用的相關資料,需要的朋友可以參考下
    2021-11-11
  • SpringBoot 如何使用Dataway配置數(shù)據(jù)查詢接口

    SpringBoot 如何使用Dataway配置數(shù)據(jù)查詢接口

    這篇文章主要介紹了SpringBoot 如何使用Dataway配置數(shù)據(jù)查詢接口,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11

最新評論

宾阳县| 武夷山市| 新宁县| 龙胜| 伊春市| 乐平市| 长海县| 天津市| 阿合奇县| 皋兰县| 桐柏县| 同德县| 平原县| 新竹县| 老河口市| 饶河县| 吉木乃县| 平定县| 合水县| 扬州市| 泽州县| 高要市| 乌兰浩特市| 蒙自县| 贵州省| 洞头县| 富源县| 望都县| 曲周县| 绥德县| 中西区| 成都市| 西城区| 应城市| 瑞金市| 永嘉县| 江华| 汪清县| 驻马店市| 巴南区| 鹰潭市|