作者自己没有采用JDBI来连接数据库,而是采用了dropwizard-hibernate 框架连接数据库,并进行处理数据。
结合官网的教程,和git-hub上的例子一步一步的整合。
1.1 导入dropwizard-hibernate 包
上一篇文章以已经提到,采用maven管理此web项目,所以先在pom文件中添加依赖项
io.dropwizard
dropwizard-hibernate
0.8.5
1.2 配置类(Configuration)添加内容
在此处贴出所有源码(有很多属性是之前项目留下的,见上一篇文章)
import io.dropwizard.Configuration;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.dropwizard.db.DataSourceFactory;
import org.hibernate.validator.constraints.NotEmpty;
public class HelloWorldConfiguration extends Configuration {
@NotEmpty
private String template;
@NotEmpty
private String defaultName = "Stranger";
@NotEmpty
private DataSourceFactory database = new DataSourceFactory();
@JsonProperty("database")
public DataSourceFactory getDataSourceFactory() {
return database;//导入配置文件中database相关的参数,在example.yml文件中
}
@JsonProperty
public String getTemplate() {
return template;
}
@JsonProperty
public void setTemplate(String template) {
this.template = template;
}
@JsonProperty
public String getDefaultName() {
return defaultName;
}
@JsonProperty
public void setDefaultName(String name) {
this.defaultName = name;
}
}
1.3 持久层Person类
import javax.persistence.*;
@Entity
@Table(name = "person")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column
private String name;
@Column
private int age;
@Column
private String email;
@Column
private String password;
//此处省略get set方法
}
1.4 数据库配置文件
关于example.yml 文件格式各位看官需要自己去了解,格式不对会影响到读入是否正确!
在example.yml文件中添加如下配置,相信各位看官一眼都能看出来这些配置代表什么意思
# Database settings.
database:
# the name of your JDBC driver
driverClass: com.mysql.jdbc.Driver
# the username
user: root
# the password
password:
# the JDBC URL
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&autoReconnect=true
properties:
hibernate.dialect: org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto: update
hibernate.format_sql: true
hibernate.showSql: true
1.5 建立PersonDao类
Dao层的作用此处不细说,直接处理数据库操作,直接上源码
import com.example.helloworld.model.Person;
import io.dropwizard.hibernate.AbstractDAO;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import java.util.List;
public class PersonDAO extends AbstractDAO {
public PersonDAO(SessionFactory factory){
super(factory);
}
public Person findById(Long id){
return get(id);
}
public long create(Person person){
return persist(person).getId();
}
@Override
protected List list(Query query) throws HibernateException {
return super.list(query);
}
@Override
protected List list(Criteria criteria) throws HibernateException {
return super.list(criteria);
}
}
1.6 新建Resource层
关于Resource层的理解,我是觉得这个Resource层和MVC模式中的Controller层很类似
import com.codahale.metrics.annotation.Timed;
import com.example.helloworld.api.Saying;
import com.example.helloworld.dao.PersonDAO;
import com.example.helloworld.model.Person;
import io.dropwizard.hibernate.UnitOfWork;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
@Path(value = "person")
@Produces(MediaType.APPLICATION_JSON)
public class PersonResource {
private PersonDAO dao;
public PersonResource(PersonDAO dao){
this.dao = dao;
}
@GET
@Timed
@UnitOfWork
public Person sayHello(@QueryParam("id") Long id) {
Person person = dao.findById(id);
return person;
}
}
1.7 应用层配置HelloWorldApplication
import com.example.helloworld.dao.PersonDAO;
import com.example.helloworld.model.Person;
import com.example.helloworld.resources.HelloWorldResource;
import com.example.helloworld.resources.PersonResource;
import io.dropwizard.Application;
import io.dropwizard.db.DataSourceFactory;
import io.dropwizard.hibernate.HibernateBundle;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
public class HelloWorldApplication extends Application {
public static void main(String[] args) throws Exception {
new HelloWorldApplication().run(args);
}
private final HibernateBundle hibernate = new HibernateBundle(Person.class) {
public DataSourceFactory getDataSourceFactory(HelloWorldConfiguration configuration) {
return configuration.getDataSourceFactory();
}
};
@Override
public String getName() {
return "hello-world";
}
@Override
public void initialize(Bootstrap bootstrap) {
bootstrap.addBundle(hibernate);
}
@Override
public void run(HelloWorldConfiguration configuration,
Environment environment) {
final HelloWorldResource resource = new HelloWorldResource(
configuration.getTemplate(),
configuration.getDefaultName());
final PersonDAO dao = new PersonDAO(hibernate.getSessionFactory());
environment.jersey().register(new PersonResource(dao));
environment.jersey().register(resource);
}
}
直接运行main方法就可以了。
下载,上一篇文章写的确实不太好,主要是对编辑器不熟悉。各位看官不要介意。