博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
idea+Spring+Mybatis+jersey+jetty构建一个简单的web项目
阅读量:4647 次
发布时间:2019-06-09

本文共 12701 字,大约阅读时间需要 42 分钟。

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

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

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

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

 

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

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

public class User {    private Integer id;    private String email;    private String password;    private String username;    public User(){    } public User(Integer id, String email, String password, String username) {        this.id = id;        this.email = email;        this.password = password;        this.username = username;    }    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getEmail() {        return email;    }    public void setEmail(String email) {        this.email = email == null ? null : email.trim();    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password == null ? null : password.trim();    }    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username == null ? null : username.trim();    }}

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

public interface IUserDao {    User login(User user);    User selectByPrimaryKey(@Param("id")Integer id);    int deleteByPrimaryKey(Integer id);    int insert(User record);    int insertSelective(User record);    int updateByPrimaryKeySelective(User record);    int updateByPrimaryKey(User record);}

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

id, email, password,userName
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}
View Code

 

六、建立Service层

  首先是UserService接口

public interface UserService {  User login(User user);  User getUserById(String id);  void add(User user);  void update(User user);  List
findByRange(int low, int high);}

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

@Service("userService")public class IUserService implements UserService {  @Autowired  private IUserDao userDao;  public User login(User user) {    return userDao.login(user);  }  public User getUserById(String id) {    return userDao.selectByPrimaryKey(Integer.parseInt(id));  }  public void add(User user) {    userDao.insert(user);  }  public void update(User user) {}  public List
findByRange(int low, int high) { return userDao.selectByRange(Integer.valueOf(low), Integer.valueOf(high)); } }

 

七、配置spring相关的xml

  applicationContext.xml的配置如下

 

八、测试数据库的类

  首先是这个测试接口,

public interface TestAPI {    User login(User user);    User findById(String id);    void Insert(User user);    pageEntity
Paging(pageEntity
pageEntity);}

  然后是实现:

public class test implements TestAPI{    @Autowired    private UserService userService;    public User login(User user){        return userService.login(user);    }    public User findById(String id) {        return userService.getUserById(id);    }    public void Insert(User user) {        userService.add(user);    }    public static void main(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);        }    }}

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

  运行后:

十、创建响应页面的jersey类

  JerseyTest类:

@Path("hello")public class JerseyTest {    @GET    @Produces("text/plain")    public String getString(){        ApplicationContext context = new ClassPathXmlApplicationContext("/applicationContext.xml");        TestAPI test =(TestAPI) context.getBean("test");        StringBuffer s = new StringBuffer();        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());        }        return s.toString();    }}

九、启动jetty

  创建StartServer类用于启动:

public class StartServer {    public static void main(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 = new ServletContextHandler(ServletContextHandler.SESSIONS);            context.addServlet(sh, "/*");            server.setHandler(context);            server.start();            server.join();        }catch (Exception e){        }    }}

十、结果

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

  得到结果:

  

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

   

 

 

 

 

转载于:https://www.cnblogs.com/xxbbtt/p/8724347.html

你可能感兴趣的文章
C#判断一个字符串是否是数字或者含有某个数字
查看>>
SVN使用指南
查看>>
【转载】掌 握 3 C ‧ 迎 接 亮 丽 职 涯
查看>>
爬取网站附件
查看>>
java基础图形界面和IO系统
查看>>
javascript学习笔记
查看>>
hdu 3996
查看>>
python第三十九课——面向对象(二)之初始化属性
查看>>
python学习笔记之函数装饰器
查看>>
FEM计算2D瞬态热传导方程
查看>>
四年时光,匆匆而过
查看>>
【php】【psr】psr1 基础编码规范
查看>>
WAF SSI
查看>>
LDAP & it's implementation
查看>>
Apache HttpComponents中的cookie匹配策略
查看>>
冰封的海盗攻略
查看>>
Netty4.x中文教程系列(四) 对象传输
查看>>
linux下find命令使用举例、
查看>>
GET请求在Tomcat中的传递及URI传递
查看>>
ubuntun 服务器与Mac
查看>>