转载:http://blog.csdn.net/u011197448/article/details/53008823
IDEA 15.0.2 + Maven3 + JDK7
第一步:搭建maven项目
当前项目结构如下图
注意JDK版本
Maven项目新建完毕,下一篇集成Spring Boot
项目结构图
1.pom文件添加SpingBoot依赖
<?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.sbm</groupId> <artifactId>SpringBoot_Maven</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.7</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.43</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version>1.2.3.RELEASE</version> </dependency> </dependencies><build><plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version>1.2.3.RELEASE</version> </dependency> </dependencies> </plugin></plugins></build> <repositories><repository> <id>spring-milestone</id> <url>https://repo.spring.io/libs-release</url></repository></repositories> <pluginRepositories><pluginRepository> <id>spring-milestone</id> <url>https://repo.spring.io/libs-release</url></pluginRepository></pluginRepositories></project>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
2.编写启动类Applaction
package com.sbmimport org.apache.ibatis.session.SqlSessionFactoryimport org.apache.tomcat.jdbc.pool.DataSourceimport org.mybatis.spring.SqlSessionFactoryBeanimport org.mybatis.spring.annotation.MapperScanimport org.mybatis.spring.mapper.MapperScannerConfigurerimport org.springframework.beans.factory.annotation.Autowiredimport org.springframework.boot.SpringApplicationimport org.springframework.boot.autoconfigure.EnableAutoConfigurationimport org.springframework.boot.autoconfigure.SpringBootApplicationimport org.springframework.boot.context.properties.ConfigurationPropertiesimport org.springframework.context.annotation.Beanimport org.springframework.context.annotation.ComponentScanimport org.springframework.core.io.support.PathMatchingResourcePatternResolverimport org.springframework.jdbc.datasource.DataSourceTransactionManagerimport org.springframework.transaction.PlatformTransactionManagerimport org.springframework.transaction.annotation.EnableTransactionManagementimport org.springframework.web.bind.annotation.RequestMappingimport org.springframework.web.bind.annotation.RestController@EnableAutoConfiguration@SpringBootApplication@ComponentScan//@RestControllerpublic class Applaction { @RequestMapping("/") public String index(){ return "Spring Boot Application..." } public static void main(String[] args) { SpringApplication.run(Applaction.class, args) }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
3.编写控制类Controller
package com.sbm.controllerimport com.sbm.service.IMessageServiceimport org.springframework.stereotype.Controllerimport org.springframework.ui.Modelimport org.springframework.web.bind.annotation.PathVariableimport org.springframework.web.bind.annotation.RequestMappingimport org.springframework.web.bind.annotation.ResponseBodyimport javax.annotation.Resourceimport java.util.Dateimport java.util.HashMapimport java.util.Map@Controllerpublic class HelloController { @RequestMapping("/hello/{name}") public String hello(@PathVariable("name") String name, Model model) { model.addAttribute("name", name) model.addAttribute("age","25") model.addAttribute("sex","man") model.addAttribute("birth",new Date()) return "hello" } @RequestMapping("/json") @ResponseBody public Map<String,Object> json(){ Map<String,Object> map = new HashMap<String,Object>() map.put("name","Flyat") map.put("age","25") map.put("sex","man") return map }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
4.测试页面访问看效果
前台页面渲染是使用的Freemarker模板,下节总结
SpringBoot 自动继承了thymeleaf、freemark、velocity三种模板技术,因为我项目中需要用来Freemark模板进行生成Java类所以,就整合了SB+F
1.配置pom文件,引入Freemarker
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId></dependency>
2.配置模板属性(application.properties)
# FREEMARKER (FreeMarkerAutoConfiguration)spring.freemarker.allow-request-override=falsespring.freemarker.allow-session-override=falsespring.freemarker.cache=falsespring.freemarker.charset=UTF-8spring.freemarker.check-template-location=truespring.freemarker.content-type=text/htmlspring.freemarker.enabled=truespring.freemarker.expose-request-attributes=falsespring.freemarker.expose-session-attributes=falsespring.freemarker.expose-spring-macro-helpers=truespring.freemarker.prefer-file-system-access=truespring.freemarker.suffix=.ftlspring.freemarker.template-loader-path=classpath:/templates/spring.freemarker.settings.template_update_delay=0spring.freemarker.settings.default_encoding=UTF-8spring.freemarker.settings.classic_compatible=truespring.freemarker.order=1
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
3.编写Controller mapping
@RequestMapping("/hello/{name}")public String hello(@PathVariable("name") String name, Model model) { model.addAttribute("name", name); model.addAttribute("age","25"); model.addAttribute("sex","man"); model.addAttribute("birth",new Date()); return "hello";}
4.编写hello.ftl页面模板
<!DOCTYPE html><html lang="en"><head> <title>SpringBoot + Freemarker</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body>Hello ${name}${age}${sex}<p>${birth?string("yyyy-MM-dd HH:mm:ss.sss")}</p></body></html>
5.启动Application、访问hello/{name}
Hello zhangyd25man2016-10-21 10:51:26.026
1.构建测试数据库(Mysql)
CREATE TABLE `message` ( `ID` int(50) NOT NULL AUTO_INCREMENT COMMENT 'ID', `NICK_NAME` varchar(50) DEFAULT NULL COMMENT '昵称', `IP` varchar(50) DEFAULT NULL COMMENT 'IP', `INSERT_TIME` datetime DEFAULT NULL COMMENT '提交时间', PRIMARY KEY (`ID`)) ENGINE=InnoDB AUTO_INCREMENT=51 DEFAULT CHARSET=utf8;
另附一个批量插入的存储过程
DELIMITER ;;CREATE DEFINER=`root`@`localhost` PROCEDURE `autoInsert`()BEGIN DECLARE i INT DEFAULT 0 ; -- 开始 SET autocommit = 0 ; -- 结束 WHILE (i < 1000) DO REPLACE INTO message ( `ID`, `NICK_NAME`, `IP`, `INSERT_TIME` ) VALUE ( i, 'zhangyd', '127.0.0.1', NOW() ) ; SET i = i + 1 ; END WHILE ; SET autocommit = 1 ; COMMIT ; END;;DELIMITER ;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
调用:call autoInsert
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.blog</groupId> <artifactId>blog</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.7</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.8</version> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.43</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version>1.2.3.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version>1.2.3.RELEASE</version> </dependency> </dependencies> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestone</id> <url>https://repo.spring.io/libs-release</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-milestone</id> <url>https://repo.spring.io/libs-release</url> </pluginRepository> </pluginRepositories></project>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
3.application.properties文件配置
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=truespring.datasource.username=rootspring.datasource.password=rootspring.datasource.driver-class-name=com.mysql.jdbc.Driver
4.程序主函数(Applaction.java)
package com.blogimport org.apache.ibatis.session.SqlSessionFactoryimport org.apache.tomcat.jdbc.pool.DataSourceimport org.mybatis.spring.SqlSessionFactoryBeanimport org.mybatis.spring.annotation.MapperScanimport org.mybatis.spring.mapper.MapperScannerConfigurerimport org.springframework.beans.factory.annotation.Autowiredimport org.springframework.boot.SpringApplicationimport org.springframework.boot.autoconfigure.EnableAutoConfigurationimport org.springframework.boot.autoconfigure.SpringBootApplicationimport org.springframework.boot.context.properties.ConfigurationPropertiesimport org.springframework.context.annotation.Beanimport org.springframework.context.annotation.ComponentScanimport org.springframework.core.io.support.PathMatchingResourcePatternResolverimport org.springframework.jdbc.datasource.DataSourceTransactionManagerimport org.springframework.transaction.PlatformTransactionManagerimport org.springframework.transaction.annotation.EnableTransactionManagementimport org.springframework.web.bind.annotation.RequestMappingimport org.springframework.web.bind.annotation.RestController@EnableAutoConfiguration@SpringBootApplication@ComponentScan//指定扫描的mapper接口所在的包@MapperScan("com.blog.mapper")//启动注解事务管理@EnableTransactionManagement//@RestControllerpublic class Applaction { private static final String TYPE_ALIASES_PACKAGE = "com.blog.model" private static final String MAPPER_LOCATION = "classpath:/mybatis/*.xml" @Bean @Autowired public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { final SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean() sqlSessionFactoryBean.setDataSource(dataSource) //mybatis.typeAliasesPackage:指定domain类的基包,即指定其在*Mapper.xml文件中可以使用简名来代替全类名(看后边的UserMapper.xml介绍) sqlSessionFactoryBean.setTypeAliasesPackage(TYPE_ALIASES_PACKAGE) sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION)) return sqlSessionFactoryBean.getObject() } public static void main(String[] args) { SpringApplication.run(Applaction.class, args) }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
*注:
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION));这一句话一定要有,就是指定程序去哪儿查找Mapper.xml文件*
5.Mapper.java、Mapper.xml
package com.blog.mapper;import com.blog.model.Message;import org.springframework.stereotype.Repository;import java.util.List;/** * blog * Created by yadong.zhang on com.blog.mapper * User:yadong.zhang * Date:2016/10/21 * Time:11:19 */@Repositorypublic interface MessageMapper{ public List<Message> findMessageInfo();}
<?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.blog.mapper.MessageMapper"> <resultMap id="message_map" type="com.blog.model.Message"> <id property="id" column="ID" jdbcType="INTEGER"></id> <result property="ip" column="IP" jdbcType="VARCHAR"></result> <result property="insertDate" column="INSERT_TIME" jdbcType="DATE"></result> <result property="nickName" column="NICK_NAME" jdbcType="VARCHAR"></result> </resultMap> <select id="findMessageInfo" resultMap="message_map"> select * from message </select></mapper>
6.Controller(中间还有Service层以及其实现,此处略)
package com.blog.controllerimport com.blog.service.IMessageServiceimport org.springframework.stereotype.Controllerimport org.springframework.ui.Modelimport org.springframework.web.bind.annotation.PathVariableimport org.springframework.web.bind.annotation.RequestMappingimport org.springframework.web.bind.annotation.ResponseBodyimport javax.annotation.Resourceimport java.util.Dateimport java.util.HashMapimport java.util.Map@Controllerpublic class HelloController { @Resource private IMessageService messageService @RequestMapping("/message") public String message(Model model){ model.addAttribute("messages", messageService.findMessageInfo()) return "message" }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
7.测试视图结果
0 0