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整个对象值

最新文章

  1. [转载]Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结
  2. Python之路,Day2 - Python基础2
  3. oracle sql语句跟踪
  4. Asp.Net 5使用第三方容器
  5. 【转】Cocos2d-x纹理优化的一些方案&mdash;&mdash;2013-08-26 22
  6. ueditor编辑器图片自定义存放目录及路径修改
  7. 【转】寻找最好的笔记软件:三强篇(EverNote、Mybase、Surfulater) (v1.0) (
  8. ListView.setOnItemClickListener无效
  9. GitHub上项目配置和导入eclispe的问题解决
  10. CVE-2017-12149 JBOOS AS 6.X 反序列化漏洞利用
  11. JQ表格隔行换色
  12. 纯css实现翻书效果
  13. erlang下lists模块sort(排序)方法源码解析(二)
  14. SpringMVC和Struts2区别比较
  15. javascript重定向页面并用post方法传递消息
  16. adb logcat查看某个进程的输出日志
  17. java Date型时间比较大小
  18. hdu 4970 trick
  19. 一些非常实用的JSON 教程
  20. [svc]通过ssh tunnel连接内网ECS和RDS

热门文章

  1. 大数据平台Hadoop集群搭建
  2. 【Django】将多个querysets拼接,使用djangorestframework序列化
  3. Spring学习(八)--Spring的AOP
  4. 决策树防止过拟合(预剪枝(Pre-Pruning))
  5. Python练习题 035:Project Euler 007:第10001个素数
  6. Linux常用命令代码大全
  7. 票房和口碑称霸国庆档,用 Python 爬取猫眼评论区看看电影《我和我的家乡》到底有多牛
  8. MCU(Micro Control Unit)中文名称为微控制单元
  9. Arduino 串行外设接口——W3Cschool
  10. Bayer Pattern——RGGB