SpringBoot 它Spring里面提供一个框架,或者一些maven的集合,(提供很多starter)
我们通过它,可以快速的构建项目 编译和部署,监控;
如果需要跑一个springweb程序,只需要添加spring-boot-starter-web依赖
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "hello world222";
}
}
@SpringBootApplication
public class SpringBootApp {
public static void main(String[] args) {
SpringApplication.run(SpringBootApp.class);
}
}
需要的依赖
<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>
<scope>providedscope>
dependency>
配置application.properties – 不要写错
# 页面默认前缀目录
spring.mvc.view.prefix=/WEB-INF/jsp/
# 响应页面默认后缀
spring.mvc.view.suffix=.jsp
需要的依赖
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-freemarkerartifactId>
dependency>
dependencies>
配置:application.properties
spring.freemarker.tempalte-loader-path=classpath:/templates
# 关闭缓存,及时刷新,上线生产环境需要修改为true
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.request-context-attribute=request
spring.freemarker.suffix=.ftl
注意一点:如果使用上面的配置 模板的名称使用templates
需要的依赖
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
dependency>
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringTestApp.class)
public class SpringTest {
@Test
public void test() throws Exception{
System.out.println(111);
}
}
需要的依赖
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
dependencies>
项目结构
public interface IUserDao {
public void save(User user);
}
@Repository
public class UserDaoImpl implements IUserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void save(User user) {
jdbcTemplate.update("insert into t_user(name) values (?)",user.getName());
}
}
需要的依赖
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.1.1version>
dependency>
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelperartifactId>
<version>4.1.0version>
dependency>
public interface UserMapper {
@Insert("insert into t_user(name) values(#{name})")
public void save(User user);
public void saveXml(User user);
//分页查询
public List<User> queryPageAll();
}
@Service
@Transactional(propagation = Propagation.SUPPORTS)
public class UserServiceImpl implements IUserService {
@Autowired
private UserMapper userMapper;
@Override
@Transactional(propagation = Propagation.REQUIRED,readOnly = false)
public void save(User user) {
userMapper.save(user);
}
@Override
public void saveXml(User user) {
userMapper.saveXml(user);
}
@Override
public List<User> queryPageAll() {
PageHelper.startPage(2,3);
List<User> users = userMapper.queryPageAll();
return users;
}
}
@SpringBootApplication
@MapperScan("cn.wing.mapper")
public class SpringMybatisApp {
public static void main(String[] args) {
SpringApplication.run(SpringMybatisApp.class);
}
}
@Configuration
public class MyBatisConfiguration {
@Bean
public PageHelper pageHelper(){
PageHelper pageHelper = new PageHelper();
Properties p = new Properties();
//配置分页的参数设置
//和startPage中的pageNum效果一样
p.setProperty("offsetAsPageNum", "true");
//设置为true时,使用RowBounds分页会进行count查询
p.setProperty("rowBoundsWithCount", "true");
// 启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页
p.setProperty("reasonable", "true");
pageHelper.setProperties(p);
return pageHelper;
}
}