这里为了自己不遗忘,做一下总结,Springboot如何配置的文章非常多,这里不详细说了,只记录一点,有可能会有找不到html或者js的情况,可以自己添加对路径的访问:
@Configuration
@EnableWebMvc
@ComponentScan
public class WebConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware{
private ApplicationContext applicationContext;
public WebConfig(){
super();
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/static/");
registry.addResourceHandler("/templates/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/templates/");
super.addResourceHandlers(registry);
}
}
重头戏是Hibernate如何配置上SpringBoot
1、application.yml(我主要配置在2中,这个看个人喜欢吧,喜欢把数据库和hibernate配置在这个文件也是可以,只是觉得这个形式太开了,不方便检查)
#app
server:
port: 8099
#spring
spring:
devtools:
restart:
enabled: true
sqlserver:
console:
enabled: true
path: /console
2、application.properties
spring.thymeleaf.encoding = UTF-8
spring.thymeleaf.mode = HTML5
spring.thymeleaf.prefix = classpath:/templates/
spring.thymeleaf.suffix = .html
spring.mvc.static-path-pattern=/static/**
spring.thymeleaf.cache = false
###SQL
spring.datasource.url = jdbc:sqlserver://127.0.0.1:1433;DatabaseName=testdb
spring.datasource.username = zl
spring.datasource.password = 123456
spring.datasource.platform = sqlserver
spring.datasource.driverClassName = com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.initialize = true
spring.datasource.continue-on-error = true
### Java Persistence Api
########################################################
# Specify the DBMS
spring.jpa.database = SQL_SERVER
#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.SQLServerDialect
#spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
3、上面文件要注意的是这个参数spring.jpa.hibernate.ddl-auto = update,不同的设置会对表进行一些不一样的操作,可以参考这个链接:https://blog.youkuaiyun.com/lixuyuan/article/details/8057119
4、配置好了就可以用了,下面只是示范代码,建议还是按更好的写全的例子走,这样才能少走弯路
@Repository
public interface VoDao extends JpaRepository<TestVo,Integer> {
@Query("select t from userinfo t where t.name=:name")
public TestVo findUserByName(@Param("name") String name);
}
@Entity //加入这个注解,就会进行持久化了
@Table(name="userinfo")
public class TestVo {
@Id
@GeneratedValue
private Integer id;
@Column(name="name")
private String name;
@Column(name="male")
private Integer male; //性别
@Column(name="age")
private Integer age; //年龄
@Column(name="hobit")
private String hobit; //爱好
public TestVo() {
}
public TestVo(String name, Integer age, Integer male,String hobit) {
this.name = name;
this.male = male;
this.hobit = hobit;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getMale() {
return male;
}
public void setMale(Integer male) {
this.male = male;
}
public String getHobit() {
return hobit;
}
public void setHobit(String hobit) {
this.hobit = hobit;
}
}
5、这里使用的SqlServer数据库,别的数据库配置可以参考链接:http://blog.chinaunix.net/uid-24648266-id-5758602.html