idea+Spring+Mybatis+jersey+jetty构建一个简单的web项目
Posted xxbbtt
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了idea+Spring+Mybatis+jersey+jetty构建一个简单的web项目相关的知识,希望对你有一定的参考价值。
一、先使用idea创建一个maven项目。
二、引入jar包,修改pom.xml
<dependencies> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-server</artifactId> <version>${jettyVersion}</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-servlet</artifactId> <version>${jettyVersion}</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-webapp</artifactId> <version>${jettyVersion}</version> </dependency> <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-server</artifactId> <version>${jerseyVersion}</version> </dependency> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet-core</artifactId> <version>${jerseyVersion}</version> </dependency> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-jetty-http</artifactId> <version>${jerseyVersion}</version> </dependency> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <!-- 开发的时候引入,发布的时候不会加载此包 --> <scope>test</scope> </dependency> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <!-- Spring-aop的两个依赖包aspectjrt&aspectjweaver --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.8.10</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.10</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.6</version> </dependency> <!-- 添加mybatis/spring整合包依赖 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <!-- DataBase数据库连接 mysql包--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>6.0.3</version> </dependency> <!-- c3p0数据源 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!-- httpClient --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.3</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api --> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.0</version> </dependency> </dependencies>
三、添加web模块,并修改web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!-- 指定Spring配置文件的位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 配置Web应用启动时候加载Spring容器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
四、先看看我已经完成的项目结构:
五、建立实体类,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,注意一下要与上面的方法名对应即可,
<?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.xw.mybatis.dao.IUserDao" > <resultMap id="BaseResultMap" type="com.xw.mybatis.entity.User" > <id column="id" property="id" jdbcType="INTEGER" /> <result column="email" property="email" jdbcType="VARCHAR" /> <result column="password" property="password" jdbcType="VARCHAR" /> <result column="userName" property="username" jdbcType="VARCHAR" /> </resultMap> <sql id="Base_Column_List" > id, email, password,userName </sql> <select id="login" resultMap="BaseResultMap" parameterType="com.xw.mybatis.entity.User"> SELECT <include refid="Base_Column_List"/> FROM t_user WHERE userName = #{username,jdbcType=VARCHAR} and password = #{password,jdbcType=VARCHAR} </select> <select id="selectByPrimaryKey" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from t_user where id = #{id,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" > delete from t_user where id = #{id,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="com.xw.mybatis.entity.User" > insert into t_user (id, email, password,userName) values (#{id,jdbcType=INTEGER}, #{email,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},#{username,jdbcType=VARCHAR}) </insert> <insert id="insertSelective" parameterType="com.xw.mybatis.entity.User" > insert into t_user <trim prefix="(" suffix=")" suffixOverrides="," > <if test="id != null" > id, </if> <if test="email != null" > email, </if> <if test="password != null" > password, </if> <if test="username != null" > userName, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="id != null" > #{id,jdbcType=INTEGER}, </if> <if test="email != null" > #{email,jdbcType=VARCHAR}, </if> <if test="password != null" > #{password,jdbcType=VARCHAR}, </if> <if test="username != null" > #{username,jdbcType=VARCHAR}, </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="com.xw.mybatis.entity.User" > update t_user <set > <if test="email != null" > email = #{email,jdbcType=VARCHAR}, </if> <if test="password != null" > password = #{password,jdbcType=VARCHAR}, </if> <if test="username != null" > userName = #{username,jdbcType=VARCHAR}, </if> </set> where id = #{id,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="com.xw.mybatis.entity.User" > update t_user set email = #{email,jdbcType=VARCHAR}, password = #{password,jdbcType=VARCHAR}, userName = #{username,jdbcType=VARCHAR} where id = #{id,jdbcType=INTEGER} </update> <select id="selectByRange" resultMap="BaseResultMap"> SELECT <include refid="Base_Column_List"/> FROM t_user WHERE id BETWEEN #{low,jdbcType=INTEGER} AND #{high,jdbcType=INTEGER} </select> </mapper>
六、建立Service层
首先是UserService接口
public interface UserService { User login(User user); User getUserB以上是关于idea+Spring+Mybatis+jersey+jetty构建一个简单的web项目的主要内容,如果未能解决你的问题,请参考以下文章
idea搭建ssm(Spring+Spring Mvc+Mybatis)
Spring+SpringMVC+Mybatis(开发必备技能)01基础idea环境配置
IntelliJ IDEA Spring+Mybatis dao bean对象注入失败