1.新建maven项目(具体的新建过程就不细说了)
2.添加maven依赖,也就是在pom.xml文件添加项目的依赖jar包:
4.0.0
springboot-test
bootTest
war
0.0.1-SNAPSHOT
bootTest Maven Webapp
http://maven.apache.org
org.springframework.boot
spring-boot-starter-parent
1.4.0.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-data-jpa
mysql
mysql-connector-java
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-thymeleaf
spring-boot-data-jpa
org.apache.maven.plugins
maven-compiler-plugin
1.8
1.8
3.在application.properties(放在resources目录下)中添加配置信息:
##JDBC Setting(Mysql的配置信息)
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/boottest?useUnicode=true&characterEncoding=utf-8spring.datasource.username=root
spring.datasource.password=root
spring.datasource.max-idle=10spring.datasource.max-wait=10000spring.datasource.min-idle=5spring.datasource.initial-size=5##服务器的配置信息
server.port=8080server.session.timeout=10server.tomcat.uri-encoding=UTF-8
##SpringDataJPA Settings
#spring.jpa.generate-ddl:true ----->将这个属性设置为TRUE,就是设置数据库自动更新,即但数据库没有实体所对应的的表时,自动创建,有对应的表时执行更新,和hibernate的hbm2ddl.auto:updata功能差不多
spring.jpa.generate-ddl: true
spring.jpa.show-sql: truespring.jpa.properties.hibernate.format_sql: false#设置模板位置,即html文件的存放位置
spring.thymeleaf.prefix=classpath:/templates/
4.项目的文件目录
5.测试
5.1 实体类User
packagecom.yxl.springboot.entity;importjavax.persistence.Entity;importjavax.persistence.GeneratedValue;importjavax.persistence.Id;importjavax.persistence.Table;
@Entity
@Table(name= "user") //声明实体对应得表,如果没有则创建(前提是application.properties文件中有相应的配置)
public classUser {
@Id
@GeneratedValueprivate intid;privateString name;privateString password;publicUser() {super();//TODO Auto-generated constructor stub
}public User(intid, String name, String password) {super();this.id =id;this.name =name;this.password =password;
}public intgetId() {returnid;
}public void setId(intid) {this.id =id;
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}publicString getPassword() {returnpassword;
}public voidsetPassword(String password) {this.password =password;
}
}
5.2 Mapper(Dao)层
packagecom.yxl.springboot.mapper;importorg.springframework.data.jpa.repository.JpaRepository;importcom.yxl.springboot.entity.User;//继承JpaRepository类
public interface UserMapper extends JpaRepository{publicUser findByName(String name);
}
5.3 controller层
packagecom.yxl.springboot.controller;importjava.util.List;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Controller;importorg.springframework.ui.Model;importorg.springframework.web.bind.annotation.RequestMapping;importcom.yxl.springboot.entity.User;importcom.yxl.springboot.mapper.UserMapper;
@Controllerpublic classSpringBootTestController {
@AutowiredprivateUserMapper userMapper;
@RequestMapping(value= "/find")publicString springBootTest(Model model) {
User findByName= userMapper.findByName("test");
model.addAttribute("user", findByName);return "test";
}
@RequestMapping(value= "/all")publicString selectAll(Model model) {
List selectAll =userMapper.findAll();
model.addAttribute("user", selectAll);return "test1";
}
}
5.4 test.html
Insert title here