IDEA 构建简单spring boot的web项目(springmvc+mybatis[mysql]+freemaker)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_26702601/article/details/83543378

记录一下IDEA 构建简单spring boot的web项目(springmvc+mybatis[mysql]+freemaker)的过程

1、项目整体目录 

2、pom.xml 添加依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.example</groupId>
   <artifactId>my-ssm-web</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>my-ssm-web</name>
   <description>Demo project for Spring Boot</description>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.0.6.RELEASE</version>
      <relativePath/>
   </parent>
   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
   </properties>
   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      <!-- mybatis支持 -->
      <dependency>
         <groupId>org.mybatis.spring.boot</groupId>
         <artifactId>mybatis-spring-boot-starter</artifactId>
         <version>1.3.2</version>
      </dependency>
      <!-- mysql驱动包 -->
      <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
         <scope>runtime</scope>
      </dependency>
      <!-- 阿里巴巴数据源 -->
      <dependency>
         <groupId>com.alibaba</groupId>
         <artifactId>druid</artifactId>
         <version>1.0.18</version>
      </dependency>
      <!-- freemarker支持 -->
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-freemarker</artifactId>
      </dependency>
   </dependencies>
   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>
</project>

3、application.properties配置

#数据源配置
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.datasource.type=com.alibaba.druid.pool.DruidDataSource

# Mybatis 映射配置
mybatis.typeAliasesPackage=com.example.demo.Domain
mybatis.mapperLocations=classpath:mapper/*.xml

#freemarker配置
spring.freemarker.allow-request-override=false
spring.freemarker.cache=true
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
spring.freemarker.prefix=/
spring.freemarker.suffix=.html
spring.freemarker.template-loader-path=classpath:/templates/

4、实体类

package com.example.demo.Domain;

import org.springframework.stereotype.Component;

@Component
public class Student {
    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

5、Dao

package com.example.demo.Dao;

import com.example.demo.Domain.Student;

public interface StudentMapper {
    int insert(Student record);
    Student selectById(Integer id);
}

6、mybatis映射文件

<?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.example.demo.Dao.StudentMapper" >
    <resultMap id="BaseResultMap" type="com.example.demo.Domain.Student" >
        <result column="name" property="name" jdbcType="VARCHAR" />
        <result column="id" property="id" jdbcType="INTEGER" />
    </resultMap>
    <insert id="insert" parameterType="com.example.demo.Domain.Student" >
        insert into user (username,id)
        values (#{username,jdbcType=VARCHAR}, #{id,jdbcType=INTEGER})
    </insert>
    <select id="selectById" parameterType="java.lang.Integer"  resultType="com.example.demo.Domain.Student" >
        select * from student where id = #{id,jdbcType=INTEGER}
    </select>
</mapper>

7、service

package com.example.demo.Service;

import com.example.demo.Domain.Student;

public interface StudentService {
    int insert(Student record);
    Student selectById(Integer id);
}

8、serviceImpl

package com.example.demo.Service.Impl;

import com.example.demo.Dao.StudentMapper;
import com.example.demo.Domain.Student;
import com.example.demo.Service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Override
    public int insert(Student record) {
        return studentMapper.insert(record);
    }
    @Override
    public Student selectById(Integer id) {
        return studentMapper.selectById(id);
    }
}

9、启动类,添加扫描dao包

扫描二维码关注公众号,回复: 3800302 查看本文章
package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.example.demo.Dao")
//此次若不定义MapperScan,也可以在dao类添加@Component和@Mapper两个注解
public class MySsmWebApplication {

   public static void main(String[] args) {
      SpringApplication.run(MySsmWebApplication.class, args);
   }
}

10、index页面简单的展示,因为使用了freemaker,所以能用freemaker表达式获取后端ModelAndView传递的参数

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>hi ${name}</h1>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_26702601/article/details/83543378
今日推荐