spring boot web实例

该博客展示了Java项目的部分代码,包含控制层、api层等。控制层使用Spring框架,有处理不同请求的方法,如返回字符串、jsp页面、json数据等;api层定义了用户服务接口。还提及了service层、appstart以及截图与访问相关内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、控制层

package com.corey.controller;

import com.corey.api.UserService;
import com.corey.model.Person;
import com.corey.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController//与springmvc不同,注解是RestController
public class AccountController {

    @Autowired
    UserService userService;

    @RequestMapping("/index")
    public String toIndex(){

        return  "hellworld!";
    }

    /**
     * 返回jsp
     * @param model
     * @return
     */
    @RequestMapping("/hello")
    public String helloJsp(Model model){
        System.out.println("HelloController.helloJsp().hello=hello");
        model.addAttribute("hello", "你好");
        return "hello";
    }

    /**
     * json格式请求
     * @return
     */
    @RequestMapping("/json")
    @ResponseBody
    public Person json(){
        Person person=new Person();
        person.setName("倪先华");
        return person;
    }

    /**
     * 连接数据库获取实体
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/getUserbyId")
    public User getUserbyId(Integer id) throws Exception{

        return userService.getUserById(id);
    }

}
、api层

package com.corey.api;

import com.corey.model.User;

public interface UserService {

    public User getUserById(Integer id) throws Exception;

}

、service层

package com.corey.service;

import com.corey.api.UserService;
import com.corey.dao.UserMapper;
import com.corey.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    public User getUserById(Integer id) throws Exception {

        return userMapper.getUserById(id);
    }
}
四、dao层
package com.corey.dao;


import com.corey.model.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

@Mapper//dao层一定要加mapper注解
public interface UserMapper {

    @Select("select * from sys_user where id = #{id}")
    public User getUserById(Integer id);
}

、appstart

package com.corey;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication(scanBasePackages="com.corey")//控制层、api、服务和dao层一定要在该包下,否则无法扫描
@MapperScan("cn.itsource.springboot.mybatis.mapper")//别名:mybatis.type-aliases-package=cn.itsource.springboot.ssm.domain//支持mybatis持久层
public class AppStart {

    public static void main(String args[]){

        SpringApplication.run(AppStart.class,args);
    }
}
五、pom.xml
<?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.corey</groupId>
    <artifactId>springboot</artifactId>
    <packaging>pom</packaging>
    <version>1.0</version>
    <modules>
        <module>common</module>
        <module>model</module>
        <module>api</module>
        <module>server</module>
        <module>web</module>
    </modules>

    <!-- 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>
        <!-- springboot版本号 -->
        <springboot.version>1.4.1.RELEASE</springboot.version>
        <!-- 指定一下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>
            <version>${springboot.version}</version>
        </dependency>
        <!--热部署-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
            <scope>true</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</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>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>

        <!--mysql,jdbc-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!--fork :  如果没有该项配置,热部署devtools不会起作用,即应用不会restart -->
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

、截图与访问

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值