SpringBoot------集成MyBatis
2021-07-14 19:07
标签:provided name .so 热部署 推荐 启动 style mapping string 1.pom.xml文件添加MyBatis和MySQL等依赖 2.修改application.properties的MySQL配置 3.添加测试类 4. 添加MyBatisMapper 5.添加MyBatisService 6.添加MyBatisController 7.添加启动类 8.添加数据库shop和表user_info 9.运行,访问地址即可 SpringBoot------集成MyBatis 标签:provided name .so 热部署 推荐 启动 style mapping string 原文地址:https://www.cnblogs.com/tianhengblogs/p/9537665.htmlproject 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/maven-v4_0_0.xsd">
modelVersion>4.0.0modelVersion>
groupId>cn.itsourcegroupId>
artifactId>springboot-shopartifactId>
packaging>warpackaging>
version>0.0.1-SNAPSHOTversion>
name>springboot-shop Maven Webappname>
url>http://maven.apache.orgurl>
parent>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-parentartifactId>
version>1.5.9.RELEASEversion>
parent>
properties>
java.version>1.8java.version>
project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
maven.compiler.encoding>UTF-8maven.compiler.encoding>
properties>
dependencies>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-webartifactId>
dependency>
dependency>
groupId>javax.servletgroupId>
artifactId>javax.servlet-apiartifactId>
scope>providedscope>
dependency>
dependency>
groupId>javax.servletgroupId>
artifactId>jstlartifactId>
dependency>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-tomcatartifactId>
scope>providedscope>
dependency>
dependency>
groupId>org.apache.tomcat.embedgroupId>
artifactId>tomcat-embed-jasperartifactId>
dependency>
dependency>
groupId>com.alibabagroupId>
artifactId>fastjsonartifactId>
version>1.2.15version>
dependency>
dependency>
groupId>mysqlgroupId>
artifactId>mysql-connector-javaartifactId>
dependency>
dependency>
groupId>org.mybatis.spring.bootgroupId>
artifactId>mybatis-spring-boot-starterartifactId>
version>1.3.1version>
dependency>
dependency>
groupId>com.github.pagehelpergroupId>
artifactId>pagehelperartifactId>
version>4.1.0version>
dependency>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-data-jpaartifactId>
dependency>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-devtoolsartifactId>
optional>trueoptional>
scope>truescope>
dependency>
dependencies>
build>
finalName>myshopfinalName>
plugins>
plugin>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-maven-pluginartifactId>
configuration>
fork>truefork>
configuration>
plugin>
plugins>
build>
project>
server.port=8080
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
#thymeleaf
#spring.thymeleaf.prefix=classpath:/templates/
#spring.thymeleaf.suffix=.html
#spring.thymeleaf.mode=HTML5
#spring.thymeleaf.encoding=UTF-8
#spring.thymeleaf.content-type=text/html; charset=utf-8
#spring.thymeleaf.cache=false
#jpa
spring.datasource.url=jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.tomcat.max-active=20
spring.datasource.tomcat.max-idle=8
spring.datasource.tomcat.min-idle=8
spring.datasource.tomcat.initial-size=10
spring.jpa.database=MYSQL
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming.strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
package myshop.bean;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import org.hibernate.annotations.GenericGenerator;
@Entity
public class UserInfo {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
package myshop.mapper;
import java.util.List;
import myshop.bean.UserInfo;
import org.apache.ibatis.annotations.Select;
public interface MyBatisMapper {
//#{name}是参数占位符
@Select("select * from user_info where username = #{username}")
public List
package myshop.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import myshop.bean.UserInfo;
import myshop.mapper.MyBatisMapper;
@Service
public class MyBatisService {
@Autowired
private MyBatisMapper myBatisMapper;
public List
package myshop.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import myshop.bean.UserInfo;
import myshop.service.MyBatisService;
@RestController
public class MyBatisController {
@Autowired
private MyBatisService myBatisService;
@RequestMapping("likeName")
public List
package myshop;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.HttpMessageConverter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
/*
* 扫描该包下相应的class,主要是MyBatis的持久化类
*
* */
@SpringBootApplication
@MapperScan("myshop.mapper")
public class App {
public HttpMessageConverters fastJsonHttpMessageConverter()
{
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastConfig = new FastJsonConfig();
fastConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
fastConverter.setFastJsonConfig(fastConfig);
HttpMessageConverter> converts = fastConverter;
return new HttpMessageConverters(converts);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(App.class, args);
}
}
http://localhost:8080/likeName?username=玉天恒
上一篇:JavaWeb实现文件下载