该工程集成spring和mybatis,使用mysql数据库。
applicationContext.xml
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://192.168.183.129:3306/mysql?useUnicode=true&characterEncoding=UTF-8
username=root
password=duckking
initialSize=0
maxActive=20
maxIdle=20
minIdle=1
maxWait=60000
用SqlSessionFactoryBean初始化数据,配置文件是configuration.xml,内容如下:
"http://mybatis.org/dtd/mybatis-3-config.dtd">
mybatis使用数据库的mapper如下:books.xml
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
现在我们建立起了一个由jersey提供api,后端集成了spring和mybatis的框架,还有一引动其它的小功能,如日志、测试等,但里面内容还是空的,现在我们向时面加内容。
@XmlRootElement
public class Book {
private Long bookId;
private String bookName;
private String publisher;
@XmlAttribute
public Long getBookId() {
return bookId;
}
public void setBookId(Long bookId) {
this.bookId = bookId;
}
@XmlAttribute
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
@XmlAttribute
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
}
@XmlRootElement(name = "books")
public class Books implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1398511916695349225L;
private List
@XmlElement(name = "book")
@XmlElementWrapper
public List
return bookList;
}
public void setBookList(List
this.bookList = bookList;
}
}
上述三个方法访问的方式分别为:
http://localhost:8080/simple-service-webapp/webapi/books
http://localhost:8080/simple-service-webapp/webapi/books/1
http://localhost:8080/simple-service-webapp/webapi/books/book?id=1
中间service层没有什么好说的,看下Dao层:
@Repository("bookDao")
public class BookDao extends SqlSessionDaoSupport {
@Autowired
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
super.setSqlSessionFactory(sqlSessionFactory);
}
public Books getAllBooks() {
List
Books books = new Books();
books.setBookList(bookList);
return books;
}
public Book findById(final String bookId) {
logger.info("bookId=" + bookId);
return (Book) this.getSqlSession().selectOne("findById", bookId);
}
}
在执行过程中可以看到后台日志:
[DEBUG] 2016-12-12 22:39:37,113 method:org.mybatis.spring.SqlSessionUtils.getSqlSession(SqlSessionUtils.java:99)
Creating a new SqlSession
[DEBUG] 2016-12-12 22:39:37,113 method:org.mybatis.spring.SqlSessionUtils.registerSessionHolder(SqlSessionUtils.java:150)
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1a753a0] was not registered for synchronization because synchronization is not active
[DEBUG] 2016-12-12 22:39:37,114 method:org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:110)
Fetching JDBC Connection from DataSource
[DEBUG] 2016-12-12 22:39:37,118 method:org.mybatis.spring.transaction.SpringManagedTransaction.openConnection(SpringManagedTransaction.java:89)
JDBC Connection [jdbc:mysql://192.168.183.129:3306/mysql?useUnicode=true&characterEncoding=UTF-8, [email protected], MySQL Connector Java] will not be managed
by Spring
[DEBUG] 2016-12-12 22:39:37,119 method:org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:145)
==> Preparing: SELECT bookid,bookname,publisher FROM book where bookid=?
[DEBUG] 2016-12-12 22:39:37,121 method:org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:145)
==> Parameters: 1(String)
[DEBUG] 2016-12-12 22:39:37,125 method:org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:145)
<== Total: 1
[DEBUG] 2016-12-12 22:39:37,125 method:org.mybatis.spring.SqlSessionUtils.closeSqlSession(SqlSessionUtils.java:193)
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1a753a0]
[DEBUG] 2016-12-12 22:39:37,126 method:org.springframework.jdbc.datasource.DataSourceUtils.doReleaseConnection(DataSourceUtils.java:327)
Returning JDBC Connection to DataSource
定义一个application类,由application类发布接口:
public class AirApplication2 extends ResourceConfig {
public AirApplication2() {
packages("com.example.resource");
}
}
将Application启动,在web.xml中配置。
一个后台服务就发布完成了,可以通过浏览器直接访问url的方式访问api了。
我们前端使用augularJS来实现,这里不讲解jQuery通过Ajax来访问后台的具体内容。
通过firefox的resteasy或rested插件来进行测试。