Spring Boot入门

使用ssh,ssm等框架写项目会发现从开始到结束一直在配置配置不停地配置,spring boot的出现,是我们脱离了繁琐的配置文件,spring boot帮我们完成了这些默认的配置

pom配置文件

<?xml version="1.0" encoding="UTF-8"?>
<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.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>dangdang</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!-- 添加Spring-data-jpa依赖. -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <!-- servlet依赖-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>


        <!--jstl标签-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <!-- tomcat 的支持.-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provide</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

启动类(一般放在根路径下)

//web工程需要继承SpringBootServletInitializer重写configure方法
//@SpringBootApplication会自动扫描此类所在包和此包的子包下的所有注解
@SpringBootApplication
public class DangdangApplication extends SpringBootServletInitializer{

    public static void main(String[] args) {
        SpringApplication.run(DangdangApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(DangdangApplication.class);
    }
}

配置文件application.properties(根路径下)

#配置数据库连接信息
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/shop
spring.datasource.username=xxxxxx
spring.datasource.password=xxxxxxx
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
#指定自定义连接池
#spring.datasource.type=org.apache.commons.dbcp2.BasicDataSource


########################################################
### Java Persistence Api --  Spring jpa\u7684\u914d\u7f6e\u4fe1\u606f.
########################################################
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
#[org.hibernate.cfg.ImprovedNamingStrategy  #org.hibernate.cfg.DefaultNamingStrategy]
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

# 页面默认前缀目录
spring.mvc.view.prefix=/WEB-INF/jsp/
#相应页面默认后缀
spring.mvc.view.suffix=.jsp

bean层省略
dao层编写(JpaRepository会在下篇讲解,只要记者他就相当于简洁的hibernate)

//CrudRepository<User,Integer>第一个参数查询类型 第二个参数主键类型
@Repository
public interface UserDao extends JpaRepository<User,Integer> {
    public User findByNicknameAndPassword(String nickname,String password);
}

service层编写

@Service
public class UserService {
    public UserService(){
        System.out.println("==============================================================================================");
    }
    @Autowired
    private UserDao userDao;
    public User login(String nickname,String password){
        return userDao.findByNicknameAndPassword(nickname,password);
    }
}

controller层编写

//@Controller
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/")
    private String index(){

        return "================json=================";
    }
    @RequestMapping("/login")
    private ModelAndView login( String nickname, String password){
        //User user=userService.login(nickname,password);
        ModelAndView mv=new ModelAndView();
        //没有此用户
        if (true){
            System.out.println("================进入Controller=============================");
            //mv.setViewName("success");
           // return mv;
            mv.setViewName("redirect:/indext.jsp");
            return mv;
        }else{
        //成功

            //request.getSession().setAttribute("user",user);
           // mv.addObject("user",request.getSession().getAttribute("user"));
            mv.setViewName("success");
            return mv;//返回视图页面
        }
    }
      @RequestMapping("/show")
    private String show(){
        return "success";//返回json字符串
    }
}

RestController注解会自动将返回的字符串解析为json文本返回(ModelAndView返回视图)
Controller注解为基于视图的注解,会把返回的字符串解析为视图路径

基于spring boot应用,由于当前应用包含了一个可直接运行的Application类,所以在开发过程中,大家很容易在IDE(IDEA工具)中单机鼠标右键运行当前类。虽然可以启动当前应用,在非web应用中可能不会有什么问题,但在web应用中,如果采用上述方法直接运行应用,那么在访问有视图的页面时(如jsp)会一直报404错误。 因为直接运行当前启动类,spring boott无法找到当前页面资源。因此,基于spring boot的应用在开发调试的时候,一定要基于spring boot提供的spring-boot-maven-plugin插件命令来运行应用或通过spring boot命令行来运行应用(spring-boot:run)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值