前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot整合mybatis、mysql、freemaker基础实现

springboot整合mybatis、mysql、freemaker基础实现

作者头像
六月的雨在Tencent
发布2024-03-28 19:51:24
810
发布2024-03-28 19:51:24
举报
文章被收录于专栏:CSDN

新建springboot项目

1.选择Spring Initializr及JDK版本点击next

2.根据个人需要修改对应名称

3.选择需要添加项

4.设置项目名称

点击Finish完成项目初始化。

5.删除搭建项目时生成的无用文件,删除后如图

配置项目信息

1.将application.properties后缀修改成application.yml

代码语言:javascript
复制
#数据库配置
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 内容如下

代码语言:javascript
复制
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内容如下:

代码语言:javascript
复制
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内容如下:

代码语言:javascript
复制
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内容如下:

代码语言:javascript
复制
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内容如下

代码语言:javascript
复制
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内容如下:

代码语言:javascript
复制
<?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文件内容如下:

代码语言:javascript
复制
p{font-size: 16px;color: mediumblue}

user.ftl文件内容如下:

代码语言:javascript
复制
<link href="../../static/common.css" rel="stylesheet" type="text/css" />
<h>  user</h>
<p>name:${user.name}</p>
<p>
    email:${user.email}
</p>

4.页面展示如下

注:以上内容如有不妥,欢迎指正。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-03-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 新建springboot项目
    • 1.选择Spring Initializr及JDK版本点击next
      • 2.根据个人需要修改对应名称
        • 3.选择需要添加项
          • 4.设置项目名称
            • 5.删除搭建项目时生成的无用文件,删除后如图
            • 配置项目信息
              • 1.将application.properties后缀修改成application.yml
                • 2.新增controller、dao、model、service包,增加如下文件
                  • 3.在resources下增加mapper包,结构如下
                    • 4.页面展示如下
                    相关产品与服务
                    云数据库 MySQL
                    腾讯云数据库 MySQL(TencentDB for MySQL)为用户提供安全可靠,性能卓越、易于维护的企业级云数据库服务。其具备6大企业级特性,包括企业级定制内核、企业级高可用、企业级高可靠、企业级安全、企业级扩展以及企业级智能运维。通过使用腾讯云数据库 MySQL,可实现分钟级别的数据库部署、弹性扩展以及全自动化的运维管理,不仅经济实惠,而且稳定可靠,易于运维。
                    领券
                    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档