Spring-boot 的使用 方便了Spring 项目的管理和维护
1 建立基本项目: https://start.spring.io/
可以选择 web, jpa ,mysql . 点击 Genarate Project 下载 .
2 导入 Eclipse 项目
解压下载的ZIp 文件到目录, 打开 Eclipse 选择 Import Existing Maven Project
点击 Next 选择 刚才解压的目录,导入就可以了。
3 目录说明:
static 缺省的静态文件目录。
templates 是模板目录。 项目缺省使用 thymeleaf 模板技术
3 运行:
找到 DemoApplication 点击右键,Run as Application 就可以了。 缺省地使用Tomcat ,大家可以使用
http://127.0.0.1:8080/ 访问
4 去掉Tomcat ,加入Jetty
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency>
5 加入 thymeleaf
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
6 加入 Mysql ,和 Hibernate 支持
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
修改 application.properties 在 resources 目录下。
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
完整的连接数据库测试:
1 建立数据库 ,Mysql 的。
CREATE TABLE `wp_site` (
`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`NAME` varchar(100) DEFAULT NULL,
`STATUS` varchar(50) DEFAULT NULL,
`CREATE_DATE` datetime DEFAULT NULL,
`CREATE_USER` varchar(50) DEFAULT NULL,
`description` varchar(500) DEFAULT NULL,
`domain_name` varchar(500) DEFAULT NULL,
`upload_path` varchar(500) DEFAULT NULL,
`title_img` varchar(500) DEFAULT NULL,
`site_path` varchar(500) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
测试路径: http://127.0.0.1:8080/greeting/test
完整项目代码 GitHub 下载:
https://github.com/guanry/wmshop.git