springBoot
一、springBoot的作用
简化spring框架的搭建;springboot有自己特定的配置方式
二、springboot继承web(jsp)
1.导包
父模块 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</version> </parent> 子模块添加spring-boot-starter-web依赖 <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> <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> </dependency>
2.配置application.properties对jsp的支持
# 页面默认前缀目录 spring.mvc.view.prefix=/WEB-INF/views/ # 响应页面默认后缀 spring.mvc.view.suffix=.jsp
3.controller层
//不适用@RestController是因为@RestController还包含@ResponBody @Controller public class JSPController { @RequestMapping("/jsp") public String jsp(Model model){ model.addAttribute("username", "只是测试一把"); return "hello"; } } 4.编写jsp页面 hello.jsp 5.运行启动服务器 @SpringBootApplication public class JSPapplication { public static void main(String[] args){ SpringApplication.run(JSPapplication.class,args); } }
三、
1.导包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
2.配置文件application.properties
//设置文件路径,程序默认去resources文件夹下面找templates,所以下面的可以不用配置 spring.freemarker.tempalte-loader-path=classpath:/templates //spring.freemarker.cache=false//缓存 //spring.freemarker.charset=UTF-8 //spring.freemarker.check-template-location=true spring.freemarker.expose-session-attributes=true//暴露session的属性 spring.freemarker.allow-session-override=true//允许session属性覆盖 spring.freemarker.suffix=.ftl//设置后缀
3.controller层
@Controller @RequestMapping("/freemarker") public class FreemarkerController { @RequestMapping("/test") public String test(Model model) { model.addAttribute("username", "怡宝测试"); return "hello"; } }
4.运行主配置类,启动服务器
@SpringBootApplication public class FreemarkerApplication { public static void main(String[] args) { springApplication.run(FreemarkerApplication.class, args); } }
四、
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- web支持: 1、web mvc; 2、restful; 3、jackjson支持; 4、aop --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency>
2.controller层代码
@SpringBootApplication public class SBTestApplication { public static void main(String[] args) { SpringApplication.run(SBTestApplication.class); } }
3.测试代码--test
@RunWith(SpringRunner.class) @SpringBootTest(classes = SBTestApplication.class) //classes路径是Application的字节码文件 public class SBTest { @Test public void method() { System.out.println("1111111"); } }
五、
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>
2.配置数据库连接--application.properties(yml也可以)
spring.datasource.username=root spring.datasource.password=root spring.datasource.url=jdbc:mysql:///crm spring.datasource.driverClassName=com.mysql.jdbc.Driver
3.三层
public interface JDBCDao { List<Employee> findAll(); }
2.Dao.Impl层
@Repository public class JDBCDaoImpl implements JDBCDao { //注入数据库操作对象JdbcTemplate @Autowired private JdbcTemplate jdbcTemplate; @Override public List<Employee> findAll() { String sql = "select id,username,password from t_employee"; List<Employee> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Employee.class)); return list; } }
3.service层
public interface JDBCService { List<Employee> findAll(); }
4.service.impl层
@Service public class JDBCServiceImpl implements JDBCService { @Autowired private JDBCDao jdbcDao; @Override public List<Employee> findAll() { return jdbcDao.findAll(); } }
5.controller层
@Controller public class JDBCController { @Autowired private JDBCService jdbcService; @RequestMapping("/findAll") public void findAll(){ jdbcService.findAll(); } }
6.Application类
@SpringBootApplication //ComponentScan:配置注解扫描,扫描@service/@Repository等 @ComponentScan("cn.itsource") public class JDBCApplication { public static void main(String[] args){ SpringApplication.run(JDBCApplication.class); } }
7.测试类
@RunWith(SpringRunner.class) @SpringBootTest(classes = JDBCApplication.class) public class JdbcTest { @Autowired private JDBCService jdbcService; @Test public void method(){ //注入service层调用方法 List<Employee> employees = jdbcService.findAll(); for (Employee e:employees) { System.out.println(e); } } }
六、
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</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>
2.配置数据库
@SpringBootApplication //扫描mapper层的接口 @MapperScan(basePackages = "cn.itsource.mapper") public class EmployeeApplication { public static void main(String[] args){ SpringApplication.run(EmployeeApplication.class); } }
2.*mapper.xml文件
<?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="cn.itsource.mapper.EmployeeMapper">
<select id="selectAll" resultType="cn.itsource.domain.Employee">
select id, username, password
from t_employee
</select>
</mapper>
3.application.properties配置文件
spring.datasource.username=root spring.datasource.password=root spring.datasource.url=jdbc:mysql:///crm spring.datasource.driverClassName=com.mysql.jdbc.Driver //*mapper.xml扫描配置 mybatis.mapper-locations=classpath:cn/itsource/mapper/*.xml
4.测试类
@RunWith(SpringRunner.class) @SpringBootTest(classes = EmployeeApplication.class) public class MyTest { @Autowired private EmployeeService employeeService; @Test public void method(){ List<Employee> employees = employeeService.findAll(); for (Employee e:employees) { System.out.println(e); } } }