码迷,mamicode.com
首页 > 编程语言 > 详细

maven搭建springboot+mybatis+freemarker

时间:2017-09-20 22:04:57      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:result   targe   marker   strong   one   web   mysq   import   release   

创建maven项目后,在pox.xml中添加依赖的jar包

技术分享
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>springboot-mybatis</groupId>
<artifactId>springboot-mybatis</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<name>springBoot-mybatis</name>
<description>Spring Boot project</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.2.RELEASE</version>
<relativePath />
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>

</dependencies>

<build>
<finalName>SpringMybatis</finalName>
<plugins>
<!-- define the project compile level -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>
View Code

新建一个Application类,用来启动web项目

package com.hushunwei;

import org.apache.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
private static Logger logger = Logger.getLogger(Application.class);

public static void main(String[] args) {
// TODO Auto-generated method stub

SpringApplication.run(Application.class, args);

logger.info("============= SpringBoot Start Success =============");
}

}

新建一个application.properties配置文件

spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
server.port=8080

 

 新建一个实体类(数据库表和字段自己创建)

package com.hushunwei.pojo;

public class User {

private int id;
private String name;
private String pwd;
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;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}

新建一个mapper接口类

sql与语句用的注解实现,注意变量名要和数据库字段相同,不然就要添加@Results注解修改字段名

package com.hushunwei.dao;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import com.hushunwei.pojo.User;

@Mapper
public interface UserMapper {
@Select("select * from user where name=#{name}")
@Results({
@Result(column="id", property="id"),
@Result(column="name", property="name"),
@Result(column="pwd", property="pwd")
})
User findUserByName(@Param("name")String name);
}

新建一个Controller类

package com.hushunwei.controller;

import java.util.Date;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.hushunwei.dao.UserMapper;
import com.hushunwei.pojo.User;

@Controller
@RequestMapping({"/home"})
public class UserController {
private Logger logger = Logger.getLogger(UserController.class);
@Autowired
UserMapper userMapper;

@RequestMapping("/user")
public String user(Map<String, Object> model) {
User user = userMapper.findUserByName("admin");
if(user != null){
logger.info("user.getAge"+user);
}
model.put("name", user.getName());
model.put("password", user.getPwd());
return "user";
}
}

在resource目录下新建一个templates文件夹,在文件下建一个user.ftl文件

<html>
<body>
账号:${name}<br/>
密码:${password}
</body>
</html>

测试结果,在浏览器输入http://localhost:8080/home/user

技术分享

技术分享

maven搭建springboot+mybatis+freemarker

标签:result   targe   marker   strong   one   web   mysq   import   release   

原文地址:http://www.cnblogs.com/hushunwei/p/7563499.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!