springboot整合mybatis、mysql、freemaker基础实现
- 1.将application.properties后缀修改成application.yml
- 2.新增controller、dao、model、service包,增加如下文件
- 3.在resources下增加mapper包,结构如下
- 4.页面展示如下
新建springboot项目
1.选择Spring Initializr及JDK版本点击next
2.根据个人需要修改对应名称
3.选择需要添加项
4.设置项目名称
点击Finish完成项目初始化。
5.删除搭建项目时生成的无用文件,删除后如图
配置项目信息
1.将application.properties后缀修改成application.yml
#数据库配置 spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/springboot?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC username: root password: 123456 #freemaker 配置 freemarker: template-loader-path: classpath:/templates suffix: .ftl #配置静态css js文件目录 mvc: static-path-pattern: /static/** #配置服务端口 server: port: 80 #配置*.xml文件 mybatis: mapper-locations: classpath:/mapper/*.xml
2.新增controller、dao、model、service包,增加如下文件
UserController.java 内容如下
package com.example.demo.controller; import com.example.demo.model.User; import com.example.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; /** * Created on 2019/5/21. */ @Controller @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @RequestMapping("/getUser") public String getUser(Model model) { User user = userService.load(1); model.addAttribute("user",user); return "user"; } }
UserDaoMapper.java内容如下:
package com.example.demo.dao; import com.example.demo.model.User; import org.apache.ibatis.annotations.Mapper; /** * Created on 2019/5/21. */ @Mapper public interface UserDaoMapper { User load(Integer id); }
User.java内容如下:
package com.example.demo.model; import java.util.Date; /** * Created on 2019/5/21. */ public class User implements java.io.Serializable{ private java.lang.Integer id; private java.lang.String avatar; private java.lang.String account; private java.lang.String password; private java.lang.String salt; private java.lang.String name; private java.util.Date birthday; private java.lang.Integer sex; private java.lang.String email; private java.lang.String phone; private java.lang.String roleid; private java.lang.Integer deptid; private java.lang.Integer status; private java.util.Date createtime; private java.lang.Integer version; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getAvatar() { return avatar; } public void setAvatar(String avatar) { this.avatar = avatar; } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getSalt() { return salt; } public void setSalt(String salt) { this.salt = salt; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Integer getSex() { return sex; } public void setSex(Integer sex) { this.sex = sex; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getRoleid() { return roleid; } public void setRoleid(String roleid) { this.roleid = roleid; } public Integer getDeptid() { return deptid; } public void setDeptid(Integer deptid) { this.deptid = deptid; } public Integer getStatus() { return status; } public void setStatus(Integer status) { this.status = status; } public Date getCreatetime() { return createtime; } public void setCreatetime(Date createtime) { this.createtime = createtime; } public Integer getVersion() { return version; } public void setVersion(Integer version) { this.version = version; } }
UserService.java内容如下:
package com.example.demo.service; import com.example.demo.model.User; /** * Created on 2019/5/21. */ public interface UserService { User load(Integer id); }
UserServiceImpl.java内容如下
package com.example.demo.service; import com.example.demo.dao.UserDaoMapper; import com.example.demo.model.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * Created on 2019/5/21. */ @Service public class UserServiceImpl implements UserService { @Autowired private UserDaoMapper userDaoMapper; @Override public User load(Integer id) { User user = userDaoMapper.load(id); return user; } }
3.在resources下增加mapper包,结构如下
UserMapper.xml内容如下:
<?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" > <!-- autogenerate V1.0 by dongao --> <mapper namespace="com.example.demo.dao.UserDaoMapper"> <!-- 返回结果Map --> <resultMap type="com.example.demo.model.User" id="BaseResultMap"> <result property="id" column="id" javaType="java.lang.Integer" jdbcType="INTEGER"/> <result property="avatar" column="avatar" javaType="java.lang.String" jdbcType="VARCHAR"/> <result property="account" column="account" javaType="java.lang.String" jdbcType="VARCHAR"/> <result property="password" column="password" javaType="java.lang.String" jdbcType="VARCHAR"/> <result property="salt" column="salt" javaType="java.lang.String" jdbcType="VARCHAR"/> <result property="name" column="name" javaType="java.lang.String" jdbcType="VARCHAR"/> <result property="birthday" column="birthday" javaType="java.util.Date" jdbcType="TIMESTAMP"/> <result property="sex" column="sex" javaType="java.lang.Integer" jdbcType="INTEGER"/> <result property="email" column="email" javaType="java.lang.String" jdbcType="VARCHAR"/> <result property="phone" column="phone" javaType="java.lang.String" jdbcType="VARCHAR"/> <result property="roleid" column="roleid" javaType="java.lang.String" jdbcType="VARCHAR"/> <result property="deptid" column="deptid" javaType="java.lang.Integer" jdbcType="INTEGER"/> <result property="status" column="status" javaType="java.lang.Integer" jdbcType="INTEGER"/> <result property="createtime" column="createtime" javaType="java.util.Date" jdbcType="TIMESTAMP"/> <result property="version" column="version" javaType="java.lang.Integer" jdbcType="INTEGER"/> </resultMap> <!--表名--> <sql id="table_name">user</sql> <!--查询字段集合--> <sql id="Base_Column_List"> id, avatar, account, password, salt, name, birthday, sex, email, phone, roleid, deptid, status, createtime, version </sql> <!--查询字段--> <sql id="Base_Column_List_asType"> id as "id", avatar as "avatar", account as "account", password as "password", salt as "salt", name as "name", birthday as "birthday", sex as "sex", email as "email", phone as "phone", roleid as "roleid", deptid as "deptid", status as "status", createtime as "createtime", version as "version" </sql> <!-- 根据主键查询用户 --> <select id="load" resultMap="BaseResultMap" parameterType="java.lang.Integer"> select <include refid="Base_Column_List"/> from <include refid="table_name"/> where id = #{id,jdbcType=BIGINT} </select> </mapper>
common.css文件内容如下:
p{font-size: 16px;color: mediumblue}
user.ftl文件内容如下:
<link href="../../static/common.css" rel="stylesheet" type="text/css" /> <h> user</h> <p>name:${user.name}</p> <p> email:${user.email} </p>
4.页面展示如下
注:以上内容如有不妥,欢迎指正。