spring-boot留言板

spring-boot留言板

概要

目的

1 使用spring boot写个留言板

2 熟悉spring boot的基本使用

详情

使用spring-boot 开发项目的前提

1 简单了解java语言
2 使用maven

一个spring-boot新手对spring-boot的理解

1 spring-boot提供了强大的功能,只需要使用maven加载依赖,很方便的开发各种应用,它集成了springmvc和spring,也可以很方便的加入orm软件
2 spring-boot 的bean都是实例化,实例化后通过spring管理
3 需要的时候可以通过依赖注入直接导入
4 常用的工具基本都已经通过框架集成,程序员只需要关注业务逻辑即可。
5 无现在还不会看dump日志,也你不太了解spring-boot和java虚拟机的运行机制,如果哪天突然挂掉,查找和定位问题会比较麻烦

项目的结构

main
    java
    resources
test
    java    

我的pomx依赖包的说明

最前面依赖 spring boot的配置

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



后面第依赖模块里面加上这些模块,模块我已经添加了注释


<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <!--不使用默认的日志记录器-->
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--测试框架-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--web模块-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--log4j2 记录日志-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>

        <!--thymeleaf 模板功能-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <!--引入MySQL连接的依赖包-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.21</version>
        </dependency>

        <!--引入jdbc支持-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <!--注解自动处理bean-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.10</version>
        </dependency>

        <!--数据jpa的支持-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <!-- 生成标准配置文档-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.2.2</version>
        </dependency>


        <!-- redis 的使用-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-redis</artifactId>
        </dependency>

        <!--spring boot的actuator监控-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

入口的main文件

1 加入注解SpringBootApplication,指定类为应用启动类
2 ServletComponentScan 控制的过滤器和监听器
@SpringBootApplication
@ServletComponentScan
public class Application {

    public static void main(String[] args){
        SpringApplication springApplication = new SpringApplication(Application.class);
        springApplication.addListeners();
        springApplication.run(args);
    }
}

类文件的设计和简单说明

@RestController rest配置
@Autowired 自动加载

@RequestMapping 直接定义了rest映射,是不是很方便
ModelAndView 定义模板类

简单说就是从数据库取数据通过模板显示数据
@RestController
public class MsgBoardController extends SuperController{

    @Autowired
    protected HttpServletRequest request;


    @Autowired
    JdbcMsgboardService jdbcMsgboardService;

    @RequestMapping(value = "/msgboard/add", method = RequestMethod.GET)
    public ModelAndView edit() throws Exception{
        ModelAndView modelAndView = new ModelAndView("msgboard/add");
        modelAndView.addObject("title","添加留言");
        return modelAndView;
    }

    @ApiOperation(value="创建留言", notes="根据用户名字,消息创建留言")
    @RequestMapping(value = "/msgboard/add", method = RequestMethod.POST)
    public void add(HttpServletResponse rsp) throws Exception{
        String name = request.getParameter("name");
        String msg = request.getParameter("msg");

        int retId = jdbcMsgboardService.createMsgboard(name, msg);
        if(retId > 0){
            rsp.sendRedirect("/msgboard/list");
        }else{
            rsp.sendRedirect("/msgboard/add");
        }
    }

    @RequestMapping(value = "/msgboard/list", method = RequestMethod.GET)
    public ModelAndView list() throws Exception{
        ModelAndView modelAndView = new ModelAndView("msgboard/list");

        int page = 1;
        String pageStr = request.getParameter("page");
        if(pageStr != null){
            page = Integer.parseInt(pageStr);
        }

        int pageSize = 20;
        String pageSizeStr = request.getParameter("pagesize");
        if(pageSizeStr != null){
            pageSize = Integer.parseInt(pageSizeStr);
        }

        List<Msgboard> list = jdbcMsgboardService.findByPage(page, pageSize);

        modelAndView.addObject("msgs",list);

        int prePage = page -1;
        if(prePage <= 0) prePage = 0;
        modelAndView.addObject("prePage", prePage);

        int nextPage = page + 1;
        if(list.size() < pageSize){
            nextPage = 0;
        }
        modelAndView.addObject("lastPage", nextPage);

        return modelAndView;
    }

    @RequestMapping(value = "/msgboard/del", method = RequestMethod.GET)
    public void del(HttpServletResponse rsp)throws Exception{
        String idStr = request.getParameter("id");
        if(idStr != null){
            int id = Integer.parseInt(idStr);
            jdbcMsgboardService.deleteMsgboardById(id);
        }
        rsp.sendRedirect("/msgboard/list");
    }
}

直接注入配置的数据

 @Value("${msgboard.test}")
private String localData;

有两种方式

1 使用spring定义的jdbctemplate
2 使用jpa

使用jpa

1 配置代码
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.jpa.properties.hibernate.hbm2ddl.auto=update
2 实际代码

    jpa写好了常用的sql,我们只需要直接复用即可
    public interface MsgboardJPARepository extends JpaRepository<MsgboardJPA, Long>{

    @Query(value="select* from msgboard order by mtime desc limit :start, :pagesize" ,nativeQuery=true)
    List<MsgboardJPA> findByPage(@Param("start") int start, @Param("pagesize") int pagesize);

}

使用redis也很容易

1 配置redis
# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring.redis.database=0
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=0
2 如果需要存储类,类需要可序列号,如果单独存储string不需要

@Configuration
public class RedisConfig extends CachingConfigurerSupport{

    @Bean
    JedisConnectionFactory jedisConnectionFactory(){
        return new JedisConnectionFactory();
    }

    @Bean
    public RedisTemplate<String, Msgboard> redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate<String, Msgboard> template = new RedisTemplate<String, Msgboard>();
        template.setConnectionFactory(jedisConnectionFactory());
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new RedisObjectSerializer());
        return template;
    }

}

项目详细说明

项目的github地址

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值