spring jersey mysql_idea+Spring+Mybatis+jersey+jetty构建一个简单的web项目

一、先使用idea创建一个maven项目。

二、引入jar包,修改pom.xml

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

org.eclipse.jetty

jetty-server

${jettyVersion}

org.eclipse.jetty

jetty-servlet

${jettyVersion}

org.eclipse.jetty

jetty-webapp

${jettyVersion}

org.glassfish.jersey.core

jersey-server

${jerseyVersion}

org.glassfish.jersey.containers

jersey-container-servlet-core

${jerseyVersion}

org.glassfish.jersey.containers

jersey-container-jetty-http

${jerseyVersion}

net.sf.json-lib

json-lib

2.4

jdk15

junit

junit

4.11

test

org.springframework

spring-core

${spring.version}

org.springframework

spring-web

${spring.version}

org.springframework

spring-oxm

${spring.version}

org.springframework

spring-tx

${spring.version}

org.springframework

spring-jdbc

${spring.version}

org.springframework

spring-aop

${spring.version}

org.springframework

spring-context-support

${spring.version}

org.springframework

spring-test

${spring.version}

org.springframework

spring-orm

${spring.version}

org.aspectj

aspectjrt

1.8.10

org.aspectj

aspectjweaver

1.8.10

org.mybatis

mybatis

3.2.6

org.mybatis

mybatis-spring

1.3.0

mysql

mysql-connector-java

6.0.3

com.mchange

c3p0

0.9.5.2

org.apache.httpcomponents

httpclient

4.5.3

javax.xml.bind

jaxb-api

2.3.0

View Code

三、添加web模块,并修改web.xml

contextConfigLocation

classpath:applicationContext.xml

org.springframework.web.context.ContextLoaderListener

四、先看看我已经完成的项目结构:

819b351fb44d1064853c06fccb2fa458.png

五、建立实体类,Mybatis的Dao接口和mapper.xml

首先是实体类,我重载了一个空的构造方法和一个需要输入所有属性的构造方法:

public classUser {privateInteger id;privateString email;privateString password;privateString username;publicUser(){

}publicUser(Integer id, String email, String password, String username) {this.id =id;this.email =email;this.password =password;this.username =username;

}publicInteger getId() {returnid;

}public voidsetId(Integer id) {this.id =id;

}publicString getEmail() {returnemail;

}public voidsetEmail(String email) {this.email = email == null ? null: email.trim();

}publicString getPassword() {returnpassword;

}public voidsetPassword(String password) {this.password = password == null ? null: password.trim();

}publicString getUsername() {returnusername;

}public voidsetUsername(String username) {this.username = username == null ? null: username.trim();

}

}

其次是IUserDao接口,这里简单的声明了普通的数据库操作方法:

public interfaceIUserDao {

User login(User user);

User selectByPrimaryKey(@Param("id")Integer id);intdeleteByPrimaryKey(Integer id);intinsert(User record);intinsertSelective(User record);intupdateByPrimaryKeySelective(User record);intupdateByPrimaryKey(User record);

}

然后是UserMapper.xml,注意一下要与上面的方法名对应即可,

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

id, email, password,userName

SELECTFROM t_user WHERE userName = #{username,jdbcType=VARCHAR} and password = #{password,jdbcType=VARCHAR}

selectfrom t_user

where id = #{id,jdbcType=INTEGER}

delete from t_user

where id = #{id,jdbcType=INTEGER}

insert into t_user (id, email, password,userName)

values (#{id,jdbcType=INTEGER}, #{email,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},#{username,jdbcType=VARCHAR})

insert into t_user

id,

email,

password,

userName,

#{id,jdbcType=INTEGER},

#{email,jdbcType=VARCHAR},

#{password,jdbcType=VARCHAR},

#{username,jdbcType=VARCHAR},

update t_user

email = #{email,jdbcType=VARCHAR},

password = #{password,jdbcType=VARCHAR},

userName = #{username,jdbcType=VARCHAR},

where id = #{id,jdbcType=INTEGER}

update t_user

set email = #{email,jdbcType=VARCHAR},

password = #{password,jdbcType=VARCHAR},

userName = #{username,jdbcType=VARCHAR}

where id = #{id,jdbcType=INTEGER}

SELECTFROM t_user

WHERE id

BETWEEN #{low,jdbcType=INTEGER}

AND #{high,jdbcType=INTEGER}

View Code

六、建立Service层

首先是UserService接口

public interfaceUserService {

User login(User user);

User getUserById(String id);voidadd(User user);voidupdate(User user);

List findByRange(int low, inthigh);

}

然后是实现UserService接口的的IUserService类,这里就已经注入Dao层的类了:

@Service("userService")public class IUserService implementsUserService {

@AutowiredprivateIUserDao userDao;publicUser login(User user) {returnuserDao.login(user);

}publicUser getUserById(String id) {returnuserDao.selectByPrimaryKey(Integer.parseInt(id));

}public voidadd(User user) {

userDao.insert(user);

}public voidupdate(User user) {}public List findByRange(int low, inthigh) {returnuserDao.selectByRange(Integer.valueOf(low), Integer.valueOf(high));

}

}

七、配置spring相关的xml

applicationContext.xml的配置如下

八、测试数据库的类

首先是这个测试接口,

public interfaceTestAPI {

User login(User user);

User findById(String id);voidInsert(User user);

pageEntity Paging(pageEntitypageEntity);

}

然后是实现:

public class test implementsTestAPI{

@AutowiredprivateUserService userService;publicUser login(User user){returnuserService.login(user);

}publicUser findById(String id) {returnuserService.getUserById(id);

}public voidInsert(User user) {

userService.add(user);

}public static voidmain(String[] args){

ApplicationContext context= new ClassPathXmlApplicationContext("/applicationContext.xml");

TestAPI test=(TestAPI) context.getBean("test");

SpringBeanUtil.getContext().getBean("test");

User user= new User(1,"xx.com","123456","xx");

User user2= test.findById("0");//根据id找数据

User user3 = test.login(user);//验证数据是否存在

System.out.println(user2.getUsername());

System.out.println(user3.getUsername());for(int i = 1; i < 20; i++){

User user= new User(i,"xx.com","123456","user"+i);

test.Insert(user);

}

}

}

运行前的数据库是这样的:

082c29987510dc93583015a571b3f156.png

运行后:

7b00ef57c933edcb25764128b017e006.png

十、创建响应页面的jersey类

JerseyTest类:

@Path("hello")public classJerseyTest {

@GET

@Produces("text/plain")publicString getString(){

ApplicationContext context= new ClassPathXmlApplicationContext("/applicationContext.xml");

TestAPI test=(TestAPI) context.getBean("test");

StringBuffer s= newStringBuffer();

s.append("username");

s.append(" " + "email ");

s.append(" " + "password");

s.append(" " + "id");for (int i = 0; i < 10; i++){

User user=test.findById(Integer.toString(i));

s.append("\n" +user.getUsername());

s.append(" " +user.getEmail());

s.append(" " +user.getPassword());

s.append(" " +user.getId());

}returns.toString();

}

}

九、启动jetty

创建StartServer类用于启动:

public classStartServer {public static voidmain(String[] args) {try{

Server server=new Server(8088);

ServletHolder sh= new ServletHolder(ServletContainer.class);

sh.setInitParameter("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jersey.api.core.PackagesResourceConfig");

sh.setInitParameter("jersey.config.server.provider.classnames",JerseyTest.class.getCanonicalName());

ServletContextHandler context= newServletContextHandler(ServletContextHandler.SESSIONS);

context.addServlet(sh,"/*");

server.setHandler(context);

server.start();

server.join();

}catch(Exception e){

}

}

}

十、结果

运行这个Server类,在浏览器中输入http://localhost:8088/hello

得到结果:

43ec014867ec147dc2580bbaa00a9f58.png

这个demo项目可以在这个链接中下载https://github.com/xbtshady/spring_mybatis

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值