web.xml文件不需要配置,pom文件要tomcat-embed-jasper中要把<scope>provided</scope>注释掉,项目启动不是server启动,是Java application启动
1、首先新增的maven项目,添加依赖如下(如下为我的pom文件):
<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.boot</groupId>
<artifactId>bootWebTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<!-- spring boot parent节点,引入这个之后,在下面和spring boot相关的就不需要引入版本了; -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
<properties>
<!-- 指定一下jdk的版本 ,这里我们使用jdk 1.8 ,默认是1.6 -->
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- web支持: 1、web mvc; 2、restful; 3、jackjson支持; 4、aop ........ -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- servlet 依赖. -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<!--JSTL(JSP Standard Tag Library,JSP标准标签库)是一个不断完善的开放源代码的JSP标签库,是由apache的jakarta小组来维护的-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- tomcat 的支持.-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<!--<scope>provided</scope> 注意,这个scope需要被注释掉.否则访问报404-->
</dependency>
<!-- 添加fastjson 依赖包. -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.51</version>
</dependency>
<!-- spring boot devtools 依赖包. -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- 添加MySQL数据库驱动依赖包. -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- spring-boot mybatis依赖:
请不要使用1.0.0版本,因为还不支持拦截器插件,
1.1.1 是博主写帖子时候的版本,大家使用最新版本即可 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- MyBatis提供了拦截器接口,我们可以实现自己的拦截器,
将其作为一个plugin装入到SqlSessionFactory中。
Github上有位开发者写了一个分页插件,我觉得使用起来还可以,挺方便的。
Github项目地址: https://github.com/pagehelper/Mybatis-PageHelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 这是spring boot devtool plugin -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--fork : 如果没有该项配置,肯呢个devtools不会起作用,即应用不会restart -->
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
2、新增boot启动类(APP)如下:
package com;
import javax.sql.DataSource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
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.context.annotation.ComponentScan;
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;
@SpringBootApplication
@ComponentScan(basePackages = {"com.bootWeb.*"})
@MapperScan("com.bootWeb.dao") //扫描:该包下相应的class,主要是MyBatis的持久化类.
public class App implements CommandLineRunner{
@Autowired
DataSource dataSource;
/**
* 在这里我们使用 @Bean注入 fastJsonHttpMessageConvert
* @return
*/
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
// 1、需要先定义一个 convert 转换消息的对象;
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//解决中文乱码问题
/*List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(fastMediaTypes);*/
//3、在convert中添加配置信息.
fastConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
}
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
/**
* 实现CommandLineRunner后查看用了什么数据库连接池
*/
public void run(String... args) throws Exception {
System.out.println("DATASOURCE = " + dataSource);
}
}
3、mybatis分页pageHelper配置(可以不写):
package com.bootWeb.config;
import java.util.Properties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.github.pagehelper.PageHelper;
/**
* MyBatis分页插件配置
* @author Administrator
*
*/
@Configuration
public class MyBatisConfiguration {
@Bean
public PageHelper pageHelper() {
System.out.println("MyBatisConfiguration.pageHelper()");
PageHelper pageHelper = new PageHelper();
Properties p = new Properties();
p.setProperty("offsetAsPageNum", "true");
p.setProperty("rowBoundsWithCount", "true");
p.setProperty("reasonable", "true");
pageHelper.setProperties(p);
return pageHelper;
}
}
4、controller类(启动时是启动的app类,不是run on server):
package com.bootWeb.controller;
import java.util.ArrayList;
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 com.bootWeb.entity.Demo;
import com.bootWeb.service.DemoService;
import com.github.pagehelper.PageHelper;
@Controller
@RequestMapping("/testWeb")
public class DemoController {
@Autowired
private DemoService demoService;
@RequestMapping("/index") //访问地址http:localhost:8080/testWeb/index
public String index(){
return "index";
}
/**
* 分页查询
* @param name
* @return
*/
@RequestMapping("/likeName") //访问地址http://localhost:8080/testWeb/likeName
public String likeName(Model model){
String name="Tim";
/*
* 第一个参数:第几页;
* 第二个参数:每页获取的条数.
*/
PageHelper.startPage(1, 2);
List<Demo> list=new ArrayList<Demo>();
list=demoService.likeName(name);
for(Demo listOne:list) {
model.addAttribute("name", listOne.getName());
break;
}
return "index";
}
@RequestMapping(value="/save",produces = "application/json; charset=utf-8")//添加produces = "application/json; charset=utf-8"解决中文乱码
public int save() {
Demo demo = new Demo();
//demo.setId(1);
demo.setName("Tom");
return demoService.save(demo);
}
}
5、service及实现类
package com.bootWeb.service;
import java.util.List;
import com.bootWeb.entity.Demo;
public interface DemoService {
List<Demo> likeName(String name);
int save(Demo demo);
}
package com.bootWeb.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.bootWeb.dao.DemoMappper;
import com.bootWeb.entity.Demo;
import com.bootWeb.service.DemoService;
@Service
public class DemoServiceImpl implements DemoService {
@Autowired
private DemoMappper demoMappper;
@Override
public List<Demo> likeName(String name) {
return demoMappper.likeName(name);
}
@Transactional//添加事务.
public int save(Demo demo) {
return demoMappper.save(demo);
}
}
6、dao接口
package com.bootWeb.dao;
import java.util.List;
import com.bootWeb.entity.Demo;
//@Mapper //使用注解后不需要扫描
public interface DemoMappper {
//@Select("select * from Demo where name=#{name}") //注解可以代替mapper.xml文件使用
public List<Demo> likeName(String name);
/**
* 保存数据
*/
//@Insert("insert into Demo(id,name) values(#{id},#{name})")
//@Options(useGeneratedKeys=true,keyProperty="id",keyColumn="id")
public int save(Demo demo);
}
7.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.bootWeb.dao.DemoMappper">
<select id="likeName" resultType="demo">
select * from Demo where name=#{name}
</select>
<insert id="save" parameterType="demo">
insert into Demo(id,name) values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR})
</insert>
</mapper>
8、实体类
package com.bootWeb.entity;
public class Demo {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
9、application.properties配置文件
#server.port=8090
logging.level.org.springframework=DEBUG
#springboot mybatis
#jiazai mybatis peizhiwenjian
mybatis.mapper-locations = classpath:mapper/*Mapper.xml
mybatis.config-location = classpath:config/mybatis-configuration.xml
mybatis.type-aliases-package = com.bootWeb.entity
########################################################
###datasource -- \u6307\u5b9amysql\u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f.
########################################################
spring.datasource.url = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
spring.datasource.username = root
spring.datasource.password = qq.123
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
#\u9875\u9762\u9ED8\u8BA4\u524D\u7F00\u76EE\u5F55
spring.mvc.view.prefix=/WEB-INF/jsp/
#\u9875\u9762\u9ED8\u8BA4\u540E\u7F00\u76EE\u5F55
spring.mvc.view.suffix=.jsp
10 、jsp文件(路径:/bootWebTest/src/main/webapp/WEB-INF/jsp/index.jsp)
<html>
<body>
<h2>Hello ${name} </h2>
</body>
</html>
11、mybatis-configuration.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
<settings>
<setting name="jdbcTypeForNull" value="NULL"/>
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
</configuration>