date: 2018-11-18 16:57:17

updated: 2018-11-18 16:57:17

1.不需要多余的配置文件信息

    application.properties
mybatis.type-aliases-package=com.mxxd.SCM.Dao
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/scm?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = spring.freemarker.template-loader-path=classpath:/template/
spring.freemarker.suffix=.ftl
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8

Dao层mapper 添加注解 @Repository

    @Repository
public interface UserMapper { //@Select("SELECT * FROM `users` where user_username = #{username} and user_password = #{password}")
public UserEntity login(String username, String password); public UserEntity queryUser(String name); public boolean insert(UserEntity user); public boolean update(UserEntity user); public boolean delete(int id); }

mapper.xml文件 添加对应mapper文件的位置

    <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.mxxd.SCM.Dao.UserMapper" > <resultMap id="userMap" type="com.mxxd.SCM.Entity.UserEntity" >
<id column="user_id" property="id" />
<result column="user_username" property="username" />
<result column="user_password" property="password" />
<result column="user_name" property="name" />
<result column="user_phone" property="phone" />
<result column="user_email" property="email" />
<result column="user_address" property="address" />
<result column="user_authority" property="authority" />
<result column="is_login" property="is_login" />
</resultMap> <select id="login" parameterType="String" resultMap="userMap">
select *
from users where user_username = #{0} and user_password = #{1}
</select> <select id="queryUser" resultMap="userMap">
select *
from users
where 1=1
<if test="id !=0">
and user_id = #{id}
</if>
<if test="username !=null and username !='' ">
and user_username = #{username}
</if>
<if test="password !=null and password !='' ">
and user_password = #{password}
</if>
<if test="name !=null and name !='' ">
and user_name like "%" #{name}"%"
</if>
</select> <insert id="insert" parameterType="com.mxxd.SCM.Entity.UserEntity" >
INSERT INTO
users
(user_username,user_password,user_name,user_phone,user_email,user_address,user_authority,is_login)
VALUES
(#{username}, #{password}, #{name},#{phone},#{email},#{address},#{authority},#{is_login})
</insert> <update id="update" parameterType="com.mxxd.SCM.Entity.UserEntity" >
UPDATE
users
SET
<if test="username != null and username != ''">user_username = #{username},</if>
<if test="password != null and password != ''">user_password = #{password},</if>
<if test="name != null and name != ''">user_name = #{name},</if>
<if test="phone != null and phone != ''">user_phone = #{phone},</if>
<if test="email != null and email != ''">user_email = #{email},</if>
<if test="address != null and address != ''">user_address = #{address},</if>
<if test="authority != null and authority != ''">user_authority = #{authority},</if>
WHERE
user_id = #{id}
</update> <delete id="delete" parameterType="Integer" >
DELETE FROM
users
WHERE
user_id =#{0}
</delete> </mapper>

Service层只需要一个service类即可 不需要一个接口一个实现类 添加注解

    @Service
@Autowired 是指自动生成get和set方法
@Service
public class UserService { @Autowired
private UserMapper userMapper; public UserEntity login(String username, String password){
UserEntity user = userMapper.login(username,password);
return user;
} }

启动类添加注解 @MapperScan("com.mxxd.SCM.Dao")自动扫描Dao层mapper

    @SpringBootApplication
@MapperScan("com.mxxd.SCM.Dao")
public class ScmApplication { public static void main(String[] args) {
SpringApplication.run(ScmApplication.class, args);
}
}

2.前端不推荐jsp,推荐thymeleaf或freemarker

使用freemarker

pom.xml文件添加依赖

    <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

前端使用 .ftl 文件

在resources文件夹下创建两个目录:static 和 template

static:目录下创建css、js、img三个目录,存放静态资源文件

template:目录下存放 XX.ftl 文件

在com.mxxd.SCM目录下创建一个Conf目录,配置springboot的静态资源文件目录

    @Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter { @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
} @Bean
public HttpMessageConverter<String> responseBodyConverter() {
StringHttpMessageConverter converter = new StringHttpMessageConverter(
Charset.forName("UTF-8"));
return converter;
} @Override
public void configureMessageConverters(
List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
converters.add(responseBodyConverter());
} @Override
public void configureContentNegotiation(
ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(false);
}
}

在 ftl 文件中如下引用

如果要引用其他页面,使用 <#include />

    <#include "header.ftl" encoding="UTF-8"/>

3.启动项目必须启动XXXApplication启动类

不能进行单元测试!!! 因为需要等SpringBoot把所有的配置全部编译完成之后才能运行,不然会找不到BeanFactory

4.测试controller层调用service调用mapper连接数据库返回值是否正确

    @Controller
@RequestMapping("/user")
public class UserController { @Autowired
private UserService userService; /*
* 启动之后访问 /user/test 会自动跳转到index.ftl
* 如果直接访问index.ftl是无法访问成功的
* 因为ftl文件是一个模板文件,需要经过后台跳转才能进行渲染成网页
*/
@RequestMapping("/test")
public String home(){
return "index";
} @RequestMapping("/home")
public ModelAndView login() {
UserEntity userEntity = userService.login("admin", "admin");
ModelAndView mv = new ModelAndView();
if (userEntity == null) {
mv.addObject("message", "用户名或密码错误,请重新输入!");
mv.setViewName("index");
} else {
mv.addObject("user", userEntity);
mv.setViewName("index");
}
System.out.println(userEntity);
System.out.println(mv.getModel());
System.out.println(mv.getViewName());
return mv;
}
}

直接在页面上进行测试,@Test 使用会报空指针错误

添加 @ResponseBody 注解不用返回页面,直接打印输出结果

前端直接使用 ${user.name} 获取user里name属性值 ${user}获取user整个对象值

Spring Boot注解与资源文件配置的更多相关文章

  1. 初识spring boot maven管理--属性文件配置

    在使用springboot的时候可以使用属性文件配置对属性值进行动态配置,官方文档原文如下: Spring Boot uses a very particular PropertySource ord ...

  2. spring boot: EL和资源 (一般注入说明(二) @Service注解 @Component注解)

    @Service用于标注业务层组件 : 将当前类注册为spring的Bean @Controller用于标注控制层组件(如struts中的action) @Repository用于标注数据访问组件,即 ...

  3. Spring读取外部的资源配置文件—@PropertySource和@Value实现资源文件配置

    通过@PropertySource可以指定读取的配置文件,通过@Value注解获取值: @PropertySource注解主要是让Spring的Environment接口读取属性配置文件用的,标识在@ ...

  4. 自定义的Spring Boot starter如何设置自动配置注解

    本文首发于个人网站: 在Spring Boot实战之定制自己的starter一文最后提到,触发Spring Boot的配置过程有两种方法: spring.factories:由Spring Boot触 ...

  5. Spring boot 默认静态资源路径与手动配置访问路径的方法

    这篇文章主要介绍了Spring boot 默认静态资源路径与手动配置访问路径的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下   在application.propertis中配置 ##端口号 ...

  6. spring boot 开静态资源访问,配置视图解析器

    配置视图解析器spring.mvc.view.prefix=/pages/spring.mvc.view.suffiix= spring boot 开静态资源访问application.proerti ...

  7. Spring boot运行原理-自定义自动配置类

    在前面SpringBoot的文章中介绍了SpringBoot的基本配置,今天我们将给大家讲一讲SpringBoot的运行原理,然后根据原理我们自定义一个starter pom. 本章对于后续继续学习S ...

  8. Spring boot集成Swagger2,并配置多个扫描路径,添加swagger-ui-layer

    Spring boot集成Swagger,并配置多个扫描路径 1:认识Swagger Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目 ...

  9. Spring boot集成Swagger,并配置多个扫描路径

    Spring boot集成Swagger,并配置多个扫描路径 1:认识Swagger Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目 ...

随机推荐

  1. Redis5设计与源码分析读后感(三)跳跃表

    一.引言 有序集合在日常开发中相当常见,比如做排名等相关的功能,肯定要用到排序的功能,那么常见底层实现有很多种: 数组 :不便于元素的插入和删除 链表 :查询效率低,需要遍历所有元素 平衡树OR红黑树 ...

  2. Java多线程--CAS

    在Java多线程并发的情况下同时对一个变量进行操作会出现线程安全的问题,假如我们现在使用20个线程对一个变量不停累加1,代码如下: 1 public class ThreadDemo implemen ...

  3. Akka Netty 比较

    从Akka出现背景来说,它是基于Actor的RPC通信系统,它的核心概念也是Message,它是基于协程的,性能不容置疑:基于scala的偏函数,易用性也没有话说,但是它毕竟只是RPC通信,无法适用大 ...

  4. 如何高雅的使用redis去获取一个值

    //场景,给定一个订单号来从缓存中查询一个订单信息; 步骤: 1从redis中直接获取,有数据就返回 2.如果redis中没有值,就查数据库 3.数据库查到的数据不为空,就刷到redis中 4.返回查 ...

  5. Book of Shaders 00 - 使用 VS Code 编写 GLSL

    0x00 写在前面 最近在学习由 Patricio 编写的 The Book of Shaders,这是一本关于 Fragment Shaders(片段着色器)的入门指南.为了在一个相对熟悉的平台运行 ...

  6. Python字符编码和二进制不得不说的故事

    二进制 核心思想: 冯诺依曼 + 图灵机 电如何表示状态,才能稳定? 计算机开始设计的时候并不是考虑简单,而是考虑能自动完成任务与结果的可靠性, 简单始终是建立再稳定.可靠基础上 经过尝试10进制,但 ...

  7. #ifdef _DEBUG #define new DEBUG_NEW #endif的解释

    转载:https://blog.csdn.net/sinat_20265495/article/details/51762738 在用vc时,利用AppWizard会产生如下代码:#ifdef _DE ...

  8. 脚手架安装react

    //1 npm install -g create-react-app //2 create-react-app xxx //xxx项目名称 //3 cd xxx //xxx项目名称 npm star ...

  9. HashMap 、ConcurrentHashMap知识点全解析

    散列表 在了解hashmap之前,要先知道什么是散列表,因为hashmap就是在散列表结构基础上改造而成的.散列表,也叫哈希表,是根据关键码值(key value)而直接进行访问的数据结构.也就是说, ...

  10. VUE 安装项目

    注意:在cmd中执行的命令 1,前提是安装了node.js 查看 npm 版本号 2,创建项目路径 mkdir vue cd vue 3,安装vue-cli (脚手架) npm install -个v ...