springboot结合ssm

springboot结合ssm

准备代码

xml


<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.0modelVersion>
  <groupId>cn.itcast.usergroupId>
  <artifactId>itcast-userartifactId>
  <version>1.0-SNAPSHOTversion>
  <parent>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-parentartifactId>
    <version>2.0.6.RELEASEversion>
  parent>
  <dependencies>
    <dependency>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starter-webartifactId>
    dependency>
  dependencies>
project>

导入springboot和wen启动器

启动类

@SpringBootApplication
public class UserApplication {
  public static void main(String[] args) {
    SpringApplication.run(UserApplication.class);
 }
}

controller

@RestController
@RequestMapping("user")
public class UserController {
  @GetMapping("hello")
  public String test(){
    return "hello ssm";
 }
}



整合spring mvc

改端口

​ springboot改配置都是在application.properties中,因为springboot默认就去该配置文件中找配置。

server.port=80	

处理静态资源

​ ResourceProperties的类,里面就定义了静态资源的默认查找路径:

  • classpath:/META-INF/resources/
  • classpath:/resources/
  • classpath:/static/
  • classpath:/public/

classpath: 指当前的目录,由于springboot读取的配置文件在resources目录,所以resources为当前路径。

​ resources/static/ 下放静态资源


编写自定义拦截器

@Component
public class MyInterceptor implements HandlerInterceptor {
 //实现相关方法
}

注册拦截器

@Configuration
public class MvcConfiguration implements WebMvcConfigurer {
  @Autowired
  private HandlerInterceptor myInterceptor;
  /**
  * 重写接口中的addInterceptors方法,添加自定义拦截器
  * @param registry*/
  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(myInterceptor).addPathPatterns("/**");
 }
}

实现接口和方法。在方法中配置相关的拦截路径




整合mybatis

导入jdbc启动器


<dependency>
  <groupId>org.springframework.bootgroupId>
  <artifactId>spring-boot-starter-jdbcartifactId>
dependency>

<dependency>
  <groupId>mysqlgroupId>
  <artifactId>mysql-connector-javaartifactId>
dependency>

配置信息

# 连接四大参数
spring.datasource.url=jdbc:mysql://localhost:3306/heima
spring.datasource.username=root
spring.datasource.password=root
# 可省略,SpringBoot自动推断
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.hikari.idle-timeout=60000
spring.datasource.hikari.maximum-pool-size=30
spring.datasource.hikari.minimum-idle=10

导入mybatis启动器


<dependency>
  <groupId>org.mybatis.spring.bootgroupId>
  <artifactId>mybatis-spring-boot-starterartifactId>
  <version>1.3.2version>
dependency>
# mybatis 别名扫描
mybatis.type-aliases-package=cn.itcast.pojo
# mapper.xml文件位置,如果没有映射文件,请注释掉
mybatis.mapper-locations=classpath:mappers/*.xm

没有配置mapper接口扫描包,给每一个Mapper接口添加 @Mapper 注解,才能被识别

使用通用mapper

@Mapper
public interface UserMapper {
}


<!-- 通用mapper -->
<dependency>
  <groupId>tk.mybatis</groupId>
  <artifactId>mapper-spring-boot-starter</artifactId>
  <version>2.0.2</version>
</dependency>
    
    
@Mapper
public interface UserMapper extends tk.mybatis.mapper.common.Mapper<User>{
}


@Service
public class UserService {
  @Autowired
  private UserMapper userMapper;
  public User queryById(Long id){
    return this.userMapper.selectByPrimaryKey(id);
 }
  @Transactional
  public void deleteById(Long id){
    this.userMapper.deleteByPrimaryKey(id);
 }
}

你可能感兴趣的:(框架学习)