如何结合jersey和mybatis
时间: 2023-11-21 17:06:33 浏览: 74
jersey+spring+mybatis整合
4星 · 用户满意度95%
结合Jersey和MyBatis的步骤如下:
1. 在pom.xml中添加Jersey和MyBatis的依赖项:
```
<dependencies>
<!-- Jersey -->
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.25.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.25.1</version>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!-- Database Driver -->
<dependency>
<groupId>com.mysql.jdbc</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
</dependencies>
```
2. 创建一个MyBatis的配置文件,例如mybatis-config.xml:
```
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mydatabase" />
<property name="username" value="myusername" />
<property name="password" value="mypassword" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/example/mybatis/mapper/MyMapper.xml" />
</mappers>
</configuration>
```
3. 创建MyMapper.xml文件,该文件定义了MyBatis映射器的SQL语句:
```
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mybatis.mapper.MyMapper">
<select id="selectUserById" parameterType="int" resultType="com.example.mybatis.User">
SELECT * FROM users WHERE id = #{id}
</select>
</mapper>
```
4. 创建一个DAO接口,例如MyDAO.java:
```
public interface MyDAO {
User selectUserById(int id);
}
```
5. 创建一个DAO实现类,例如MyDAOImpl.java:
```
public class MyDAOImpl implements MyDAO {
private SqlSessionFactory sqlSessionFactory;
public MyDAOImpl() {
String resource = "com/example/mybatis/config/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
@Override
public User selectUserById(int id) {
try (SqlSession session = sqlSessionFactory.openSession()) {
MyMapper mapper = session.getMapper(MyMapper.class);
return mapper.selectUserById(id);
}
}
}
```
6. 创建一个Jersey资源类,例如MyResource.java:
```
@Path("/users")
public class MyResource {
private MyDAO dao;
public MyResource() {
dao = new MyDAOImpl();
}
@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public User getUserById(@PathParam("id") int id) {
return dao.selectUserById(id);
}
}
```
7. 将Jersey资源类注册到Jersey应用程序中,例如MyApplication.java:
```
@ApplicationPath("/api")
public class MyApplication extends ResourceConfig {
public MyApplication() {
register(MyResource.class);
}
}
```
8. 部署应用程序并启动服务器,然后访问http://localhost:8080/api/users/1将返回id为1的用户的JSON表示。
阅读全文