Create a new project in the previous article.
Step two: Create the table and the corresponding entity class
Entity class: User.java
Packagecom.qtt.im.entity;Importjava.io.Serializable; Public classUserImplementsSerializable {PrivateLong ID; PrivateString LoginName; PrivateString Pass; PrivateString Mobile; PrivateString Email; PrivateString name; PublicLong getId () {returnID; } Public voidsetId (Long id) { This. ID =ID; } PublicString Getloginname () {returnLoginName; } Public voidsetloginname (String loginName) { This. LoginName =LoginName; } PublicString Getpass () {returnPass; } Public voidSetPass (String pass) { This. pass =Pass; } PublicString Getmobile () {returnMobile; } Public voidSetmobile (String mobile) { This. Mobile =Mobile; } PublicString Getemail () {returnemail; } Public voidsetemail (String email) { This. email =email; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; }}Step three: Create a write Mapper interface, Service,controller
Take a query as an example:
Usermapper.java (This interface is used for connections between XML files and service)
Package Com.qtt.im.mapper; Import Com.qtt.im.entity.User; Import org.apache.ibatis.annotations.Mapper; @Mapper Public Interface Usermapper { public User getuserlist ();}
Userservice.java
Package Com.qtt.im.service; Import Com.qtt.im.entity.User; Public Interface UserService { public User getuserlist ();}
Userserviceimpl.java (Usermapper will error, but does not affect the operation)
PackageCom.qtt.im.service.impl;ImportCom.qtt.im.entity.User;ImportCom.qtt.im.mapper.UserMapper;ImportCom.qtt.im.service.UserService;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.stereotype.Service; @Service Public classUserserviceimplImplementsUserService {@AutowiredPrivateUsermapper Usermapper; PublicUser getuserlist () {returnusermapper.getuserlist (); }}
Usercontroller.java
PackageCom.qtt.im.controller;ImportCom.qtt.im.entity.User;ImportCom.qtt.im.service.UserService;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RestController; @RestController @requestmapping (Value= "/user") Public classUsercontroller {@AutowiredPrivateUserService UserService; @RequestMapping ("/getuserlist") PublicUser getuserlist () {User userlist=userservice.getuserlist (); returnuserlist; }}
The annotations you use can be queried online.
Fourth step: You can add some filters
Myfilter.java
PackageCom.qtt.im.filter;Importjavax.servlet.*;Importjavax.servlet.http.HttpServletRequest;Importjava.io.IOException; Public classMyfilterImplementsFilter {@Override Public voidInit (Filterconfig filterconfig)throwsservletexception {} @Override Public voidDoFilter (ServletRequest servletrequest, Servletresponse servletresponse, Filterchain filterchain)throwsIOException, servletexception {httpservletrequest request=(httpservletrequest) ServletRequest; System.out.println ("This is my filter URL:" +Request.getrequesturi ()); Filterchain.dofilter (Servletrequest,servletresponse); } @Override Public voiddestroy () {}}
Webconfiguration.java
PackageCom.qtt.im.filter;ImportOrg.apache.catalina.filters.RemoteIpFilter;ImportOrg.springframework.boot.web.servlet.FilterRegistrationBean;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration; @Configuration Public classwebconfiguration {@Bean PublicRemoteipfilter Remoteipfilter () {return NewRemoteipfilter (); } @Bean PublicFilterregistrationbean testfilterregistration () {Filterregistrationbean Filterregistrationbean=NewFilterregistrationbean (); Filterregistrationbean.setfilter (Newmyfilter ()); Filterregistrationbean.addurlpatterns ("/*"); Filterregistrationbean.addinitparameter ("ParamName", "Paramvalue"); Filterregistrationbean.setname ("Myfilter"); Filterregistrationbean.setorder (1); returnFilterregistrationbean; }}
Fifth step: Write a program entry
Application.java
Packagecom.qtt.im;ImportOrg.mybatis.spring.annotation.MapperScan;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication, @SpringBootApplication @mapperscan ("Com.qtt.im.mapper") Public classApplication { Public Static voidMain (string[] args) {springapplication.run (application.class, args); }}
The structure diagram of the Java Code section is as follows:
Sixth step: Create an XML file that writes SQL statements
<?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 "><Mappernamespace= "Com.qtt.im.mapper.UserMapper" > <SelectID= "Getuserlist"Resulttype= "Com.qtt.im.entity.User" >SELECT id,loginname from Im_user; </Select></Mapper>
Where namespace: Is the Java part of the Usermapper interface. Resulttype: is the entity class. The return type.
Seventh Step: Create a configuration file
Application.proterties
spring.datasource.url=jdbc:mysql://localhost:3306/sdt?useunicode=true&characterencoding=utf-8 (SDT is a database)Mybatis.type-aliases-package=com.qtt.im.mapper (Mapper interface scan)Spring.datasource.driver-class-name = Com.mysql.jdbc.Driver (drive)Spring.datasource.type=com.alibaba.druid.pool.druiddatasourcespring.datasource.username=root (user name)Spring.datasource.password=qintongtong (password)mybatis.mapper-locations=classpath*:/mappers/**mapper.xml (XML file)
Log4j.properties
Log4j.rootlogger=warn, consolelog4j.appender.console= org.apache.log4j.consoleappenderlog4j.appender.console.layout= ORG.APACHE.LOG4J.PATTERNLAYOUTLOG4J.APPENDER.CONSOLE.LAYOUT.CONVERSIONPATTERN=%D{HH:MM:SS,SSS} [%t]%-5p%C{1}:%m %n
The structure of the configuration file is shown below:
Note: The directory where the Usermapper.xml file is located must be set as follows: File--->project structtre-->moudles
If not set, the XML file will not be found, or can be placed directly under the resources, but not easy to manage.
Eighth step: Start the project test
After starting this class, enter the address in the browser: After success, the following
User and getuserlist are what you define in the controller layer
Springboot MyBatis Integration