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网站.