记录一下IDEA 构建简单spring boot的web项目(springmvc+mybatis[mysql]+freemaker)的过程
1、项目整体目录
2、pom.xml 添加依赖
4.0.0 com.example my-ssm-web 0.0.1-SNAPSHOT jar my-ssm-web Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.0.6.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.2 mysql mysql-connector-java runtime com.alibaba druid 1.0.18 org.springframework.boot spring-boot-starter-freemarker org.springframework.boot spring-boot-maven-plugin
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映射文件
insert into user (username,id) values (#{username,jdbcType=VARCHAR}, #{id,jdbcType=INTEGER})
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包
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传递的参数
Title hi ${name}