Import Com.example.demo2.com.example.dao.shopdao;import Com.example.demo2.com.example.entity.shops;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.responsebody;import Org.springframework.web.bind.annotation.restcontroller;import Javax.annotation.resource;import java.util.List;@ Restcontrollerpublic class Shopcontroller {@Autowired private Shopdao Shopdao; /* * Query * */@RequestMapping ("Sele") @ResponseBody public List test () {list<shops> finl = Shop Dao.finalall (); for (shops shop:finl) {System.out.println (Shop.getnames ()); System.out.println (Shop.getpass ()); } return FINL; }/* * add * */@RequestMapping ("add") @ResponseBody public string test1 (string names,string pass,string Sex) {Shops shop = new shops (); Shop.setnames (names); Shop.setpass (pass); Shop.setsex (Sex); Integer result = Shopdao.add (shop); String st = "Add failed"; if (result>0) {st = "add success"; } return St; }/* * Modify * */@RequestMapping ("UPDA") @ResponseBody public String test2 (Integer id,string Names,stri ng pass,string Sex) {Shops shop = new shops (); Shop.setid (ID); Shop.setnames (names); Shop.setpass (pass); Shop.setsex (Sex); Integer result = Shopdao.updates (shop); String st = "Modification failed"; if (result>0) {st = "modified successfully"; } return St; }/* * Delete * */@RequestMapping ("dele") @ResponseBody public String test3 (integer ID) {integer r Esult = shopdao.deletes (ID); String st = "Delete failed"; if (result>0) {st = "Delete succeeded"; } return St; }}
DAO layer
ImportCom.example.demo2.com.example.entity.Shops;ImportOrg.apache.ibatis.annotations.Mapper;Importorg.springframework.stereotype.Repository;Importjava.util.List; @Mapper//The declaration is a mapper, and the @mapperscan two in Springbootapplication can be written on it . Public InterfaceShopdao {//Enquiry PublicList<shops>Finalall (); //Add PublicInteger Add (shops shop); //Modify PublicInteger Updates (shops shop); //Delete Publicinteger Deletes (integer ID);}
Mapper
<?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.example.demo2.com.example.dao.ShopDao" > <select id= "finalall" resulttype= " Com.example.demo2.com.example.entity.Shops ">Select* from First</select> <insert id= "Add" parametertype= "Com.example.demo2.com.example.entity.Shops" usegeneratedkeys= " True ">INSERT into first (names,pass,sex) VALUES (#{names},#{pass},#{sex})</insert> <update id= "Updates" parametertype= "Com.example.demo2.com.example.entity.Shops" >Update first set names=#{names},pass=#{pass},sex=#{sex} WHERE id =#{id}</update> <delete id= "deletes" parametertype= "Com.example.demo2.com.example.entity.Shops" >Delete from first where ID=#{id}</delete> </mapper>
Application.yml
Server:port:8088Context-path:/Spring:profiles:active:dev Application:name:demo-2jackson:date-format:yyyy-mm-DD HH:mm:ss time-zone:gmt+8default-property-inclusion:non_null Datasource:url:jdbc:mysql://Localhost:3306/test?useunicode=true&characterencoding=utf8username:root Password:root # using Druid data source Type:com.alibaba.druid.pool.DruidDataSource Driver-class-name:com.mysql.jdbc.Driver filters:stat maxactive:20initialsize:1maxwait:60000Minidle:1Timebetweenevictionrunsmillis:60000Minevictableidletimemillis:300000Validationquery:select' X 'Testwhileidle:trueTestonborrow:falseTestonreturn:falsepoolpreparedstatements:truemaxopenpreparedstatements:20Mybatis:typeAliasesPackage:com.example.demo2.com.example.dao MapperLocations:classpath:mapper/*. XML
Project Startup file
ImportOrg.mybatis.spring.annotation.MapperScan;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication;Importorg.springframework.transaction.annotation.enabletransactionmanagement;@ Springbootapplication@enabletransactionmanagement//Turn on transaction management@MapperScan ("Com.example.demo2.com.example.dao")//with the DAO layer of the @mapper two select write on it (the main function is to sweep the package) Public classdemo2application { Public Static voidMain (string[] args) {Springapplication.run (demo2application.class, args); }}
Pom file
<?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>demo-2</artifactId> < Version>0.0.1-snapshot</version> <packaging>jar</packaging> <name>demo-2</name> <description>demo Project forSpring boot</description> <parent> <groupId>org.springframework.boot</groupId> &L T;artifactid>spring-boot-starter-parent</artifactid> <version>1.5.9.RELEASE</version> & Lt;relativepath/> <!--lookup parent from repository to </parent> <properties> <proj Ect.build.sourceencoding>utf-8</project.build.sourceencoding> <project.reporting.outputEncoding> Utf-8</project.reporting.outputencoding> <java.version>1.8</java.version>
<!--This can be used with spring boot MyBatis two X---<!--<mybatis.version>3.3.1</mybatis.version>--& Gt <!--<mybatis.spring.version>1.2.4</mybatis.spring.version>--> </properties> < Dependencies> <dependency> <groupId>org.springframework.boot</groupId> & Lt;artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <ARTIFACTID>SPRING-BOOT-STARTER-AOP</ARTIFAC tid> </dependency> <dependency> <groupid>org.springframework.boot</groupi D> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <!-- Druid-<dependency> <groupId>com.alibaba</groupId> <artifactid> ;d ruid</artifactid> <version>1.0.11</version> </dependency> <dependency> <grou Pid>mysql</groupid> <artifactId>mysql-connector-java</artifactId> <scope> runtime</scope> </dependency> <dependency> <groupid>org.springframework.b Oot</groupid> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!--Mybatis Two---<!--<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> & lt;/dependency> <dependency> <groupId>org.mybatis</groupId> <artifact Id>mybatis-spring</artifactid> <version>${mybatis.spring.version}</version> </de Pendency>-->
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactid>mybatis- Spring-boot-starter</artifactid> <version>1.1.1</version> </dependency> & lt;! --Mybatis Generator--<dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> <scope>compile</scope> <optional>true</optional> </dependency> <dependency> <groupid>org.springframework.boot& Lt;/groupid> <artifactId>spring-boot-starter-test</artifactId> <scope>test</ scope> </dependency> </dependencies> <build> <plugins> <plugi N> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot- maven-plugin</artifactid> </plugin> </plugins> </build></project>
Spring Boot Simple Mini demo (for Beginners)