Maven--搭建Web项目

1 之前搭项目一直都是采用添加架包的形式,学了一下Maven,尝试采用Maven搭Web项目。
将Dao层和Service层和Controller分层,分到三个不同的项目中,通过依赖来连接三个不同的层次。

2 Dao层,即数据访问层,创建一个Maven项目
2.1 生成Maven项目
这里写图片描述
这里写图片描述
这里写图片描述
填写说明:Group Id为项目名称,而Artifact ID为子项目名称。比如Spring即为Group Id,而Artifact ID为spring-context,即主从的关系。版本自己定义。Package为帮助你提前生成一个包。可随意填写,生成后可更改。
2.2 Finish后会看到如下的项目结构
这里写图片描述
2.3 更改jdk版本,默认生成的不知道是啥编译版本,改成自己想要的版本
这里写图片描述
2.4 创建配置文件文件夹
这里写图片描述
放在该下的配置文件均用classpath:来访问。并且部署后apache-tomcat-8.0.35\webapps\user-web\WEB-INF\classes下。
生成文件目录如下:
这里写图片描述
pom中添加设计到的架包

<dependencies>
    <!-- Mybatis-->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.4.4</version>
    </dependency>

    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.10.RELEASE</version>
    </dependency>

    <!-- 日志 -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

    <!-- 测试 -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
  </dependencies>

Mapper.xml文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="com.text.dao.UserMapper" >
  <resultMap id="BaseResultMap" type="com.text.pojo.User" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="name" property="name" jdbcType="VARCHAR" />
    <result column="password" property="password" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, `name`, `password`
  </sql>
<select id="selectByExample" resultMap="BaseResultMap" parameterType="com.text.pojo.UserExample" >
    select
    <if test="distinct" >
      distinct
    </if>
    <include refid="Base_Column_List" />
    from `user`
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null" >
      order by ${orderByClause}
    </if>
  </select>

映射的接口:

package com.text.dao;

import com.text.pojo.User;
import com.text.pojo.UserExample;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper {
     /**
     * 批量条件查询
     * 参数:查询条件,null查整张表
     * 返回:对象集合
     * @ibatorgenerated 2017-02-07 23:33:19
     */
    List<User> selectByExample(UserExample example);
}

实体:

package com.text.pojo;
import java.io.Serializable;
public class User implements Serializable {

    private Integer id;

    private String name;

    private String password;
    //get set 省略

3 Service层
和上面一样,均生成一个Maven项目
pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.java.1234.user</groupId>
  <artifactId>user-service</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>user-service</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>

    <!-- 依赖本地的Dao层项目 -->
    <dependency>
        <groupId>com.java.1234.user</groupId>
        <artifactId>user-dao</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>

    <!-- 测试 -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>

  </dependencies>
</project>

Service依赖Dao成中的类,所以要在pom.xml中引用
这里写图片描述
Service中的添加两个包:一个com.text.service.impl.user,一个com.text.service.user

package com.text.service.user;

import java.util.List;

import com.text.pojo.User;

public interface UserService {

    /**
     * 根据用户条件进行查询
     */
    public List<User> getUserInfoByCondition(User user);
}

package com.text.service.impl.user;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.text.dao.UserMapper;
import com.text.pojo.User;
import com.text.pojo.UserExample;
import com.text.pojo.UserExample.Criteria;
import com.text.service.user.UserService;
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    /**
     * 根据用户条件进行查询
     */
    public List<User> getUserInfoByCondition(User user){
        UserExample example=new UserExample();
        Criteria criteria=example.createCriteria();
        criteria.andNameEqualTo(user.getName());
        criteria.andPasswordEqualTo(user.getPassword());
        return userMapper.selectByExample(example);
    }
}

4 Controller层,生成Maven项目和上面有点不一样,在第三步的位置
这里写图片描述
生成项目后替换两个文件
这里写图片描述
pom.xml中引入需要的架包,通过添加Service层依赖包,如下只贴的部分代码:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.java.1234.user</groupId>
  <artifactId>user-web</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>user-web</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <!-- Server层依赖-->
    <dependency>
        <groupId>com.java.1234.user</groupId>
        <artifactId>user-service</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>

  </dependencies>
  <build>
    <finalName>user-web</finalName>
  </build>
</project>

这里写图片描述
创建resources文件夹存放配置文件,如spring-mvc.xml和applicationContext.xml
创建com.text.controller.user包,添加一个控制类:

@RequestMapping("/user")
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 验证登录
     * @param response
     * @param request
     * @param user
     * @throws IOException
     */
    @RequestMapping("/login")
    public String login(HttpServletResponse response,HttpServletRequest request,User user) throws IOException{
        List<User> lstUser=userService.getUserInfoByCondition(user);
        if(lstUser!=null&&lstUser.size()>0){
            request.setAttribute("message", "success");
        }else{
            request.setAttribute("message", "error");
        }
        return "index";
    }
}

index.jsp页面:

<body>
    <form  id="form01" action="<%=basePath %>user/login.html" enctype="multipart/form-data" >
        <input type="text" name="name" id="name"/> 
        <input type="password" name="password" id="password"/>
        <input type="submit" value="确认">
    </form>
    ${message }
  </body>

效果:
这里写图片描述
附出现的警告及报错:
警告:Establishing SSL connection without server’s identity verification is not recommended.
解决方案:在mysql连接字符串url中加入ssl=true或者false即可,如下所示。
url=jdbc:mysql://127.0.0.1:3306/framework?characterEncoding=utf8&useSSL=true
参考:https://zhidao.baidu.com/question/2056521203295428667.html

applicationContext.xml报错:The reference to entity “useSSL” must end with the ‘;’ delimiter.
连接jdbc时,路径链接参数之前一直是使用&符号连接,但是这次却 提示用;来进行连接,wtf!
于是 按照编译器的提示 我把&改为;,于是 运行 起来它把两个参数 当做了一个参数,OhMyGod!
在没头没脑的一番探索后,终于发现问题,在xml文件中 &符号 需要转义 这个根据 HTML 的转义规则 更改就行
& -> & 于是便解决了
如:jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=true
参考:http://www.zhimengzhe.com/shujuku/MySQL/282953.html
下载位置:http://download.csdn.net/download/btwangzhi/9941189
学习自某java1234网站.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值