1、这里是 pom.xml 配置,这里主要集成了mybatis和freemarker
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 这个是spring boot启动开发者模式,当修改类文件它会自动帮我们重启一部分更新的内容 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
</dependencies>
<!-- spring boot打包工具,这里不需要指出main方法,还有需要引入的资源 ,spring_boot 就可以直接给我们打包 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2、然后就是最主要的application.properties 配置,当然这里还可以用其他的配置,我这里就只讲我自己的配置
#这里可以配置你mapper.java 的包路径配置,这个也可以在启动的时候加上注解扫描配置 @MapperScan("com.cmpyun.wx.dao")
mybatis.type-aliases-package=com.yang.mapper
mybatis.config-locations=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password =root
spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
# 设置容器的编码
server.tomcat.uri-encoding=UTF-8
# 设置web服务名称
server.name=springBoot
# 设置服务端口
server.port=8080
# 设置 freemarker 文件路径
spring.freemarker.template-loader-path=classpath:/templates
# 配置静态文件路径
spring.mvc.static-path-pattern=/static/**
########################################################
###FREEMARKER (FreeMarkerAutoConfiguration)
########################################################
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.request-context-attribute=
#spring.freemarker.settings.*=
spring.freemarker.defaultEncoding=UTF-8
spring.freemarker.freemarkerSettings.number_format=0.##
spring.freemarker.suffix=.html
spring.freemarker.template-loader-path=classpath:/templates/
#comma-separated list
#spring.freemarker.view-names= # whitelist of view names that can be resolved
3、mybatis-config.xml 这里我没有深入配置,分页什么的都没有配置,需要用的话,请查阅mybatis 相关文档
<configuration>
<typeAliases>
<typeAlias alias="Integer" type="java.lang.Integer" />
<typeAlias alias="Long" type="java.lang.Long" />
<typeAlias alias="HashMap" type="java.util.HashMap" />
<typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />
<typeAlias alias="ArrayList" type="java.util.ArrayList" />
<typeAlias alias="LinkedList" type="java.util.LinkedList" />
</typeAliases>
</configuration>
4、这里是mapper 文件 注意namespace 是dao的包名加类名
<?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.yang.mapper.StuentMapper" >
<insert id="insert">
insert into stuent (
name,classId,age,createTime
)
values(
#{name},#{classId},#{age},now()
)
</insert>
<select id="findStuent" resultType="java.util.HashMap" >
select * from stuent
</select>
</mapper>
5、app 启动类
// 注意这个地方最好 用SpringBootApplication这个注解,不然静态文件路径就不好用了,具体原因这里就不讲解了,网上有很多
@MapperScan("com.yang.mapper")
@SpringBootApplication
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}
6、这个是一个简单的 dao,这里就只需要写一个接口就可以了
public interface StuentMapper {
List<Map> findStuent();
void insert(Map stuent);
}
7、最后是一个Control
@Controller // @RestController //相当于Responsebody和Controller
public class IndexController {
@Autowired
private StuentMapper stuentMapper;
@RequestMapping("/")
String home() {
return "Hello World!";
}
// @ResponseBody templates
@RequestMapping("/index")
String index() {
return "index";
}
@RequestMapping("/addStuent")
String addStuent(String name,int age,int classId) {
Map stuent = new HashMap();
stuent.put("name", name);
stuent.put("age", age);
stuent.put("classId", classId);
stuent.put("createTime", new Date());
stuentMapper.insert(stuent);
return stuent.toString();
}
@RequestMapping("/findStuent")
List<Map> findStuent() {
List<Map> students = stuentMapper.findStuent();
return students;
}
}
这样就完成了所以的编写,是不是很简单啊,本人觉得最赞的地方就是dao就主需要写一个接口就可以了,就可以完成数据库的增删改查,大大的减少了工作量