1.总的目录
2.导包(我直接导入了常用的,有些是多余的)
打包下载:链接:http://pan.baidu.com/s/1bpimPB5 密码:yphy
说明:画了框框的那两个包一定要导对版本,另外有个包名为commons-dbcp-dbcp2xxxxx.jar 的,跟上面的一个画框框中的一个很像,不要导错了。(后期加入:很多数据库连接池的包都可以,唯独commons-dbcp-dbcp2xxxxx.jar会报错)
3.数据库表
CREATE TABLE USER( id VARCHAR(20) PRIMARY KEY, NAME VARCHAR(20), job VARCHAR(20) );
4.web.xml
配置 springmvc 配置文件的位置,配置编码过滤器
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>CRUD</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- The front controller of this Spring Web application, responsible for handling all application requests --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Map all requests to the DispatcherServlet for handling --> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 配置编码过滤器 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
5.springmvc 配置文件 spring-mvc.xml
配置自动扫面的包(controller) 和 springMVC 视图解析器
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <context:component-scan base-package="com.loger.controller"></context:component-scan> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
6.spring 的配置文件 applicationContext.xml
1>配置数据源(org.apache.commons.dbcp2.BasicDataSource),不要导错包
2>配置 SqlSessionFaction,属性包括数据源和 mybatis 的 mapper 的文件位置
3> 配置 mybatis 注解 ,用此注解来标识 mybatis 的接口方法,使得该接口注入到 Spring 的 IOC 容器中,并且该接口的方法与 mapper 的各种操作对应。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 配置数据源 --> <bean class="org.apache.commons.dbcp2.BasicDataSource" id="dataSource"> <property name="url" value="jdbc:mysql://localhost:3306/crud"></property> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="username" value="root"></property> <property name="password" value="apple"></property> </bean> <!-- 配置SqlSessionFaction --> <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory"> <property name="dataSource" ref="dataSource"></property> <property name="mapperLocations" value="com/loger/entity/*.xml"></property> </bean> <!-- 配置 mybatis 注解 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.loger.dao"></property> <property name="annotationClass" value="com.loger.annotation.MyBatisRepository"></property> </bean> </beans>
7. 建立 mybatis 注解接口 MyBatisRepository.java
package com.loger.annotation; /** * Mybatis 使用该注解标识 DAO * @author Loger * */ public @interface MyBatisRepository { }
8. 实体类 User.java
package com.loger.entity; public class User { private String id; private String name; private String job; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", job=" + job + "]"; } public User(String id, String name, String job) { super(); this.id = id; this.name = name; this.job = job; } public User() { super(); } }
mybatis 的 mapper 文件 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"> <!-- namespace对应接口的全类名 --> <mapper namespace="com.loger.dao.UserDao"> <select id="findAll" resultType="com.loger.entity.User"> select * from user </select> <select id="findById" resultType="com.loger.entity.User"> select * from user where id = #{id} </select> <delete id="deleteById" parameterType="int"> delete from user where id = #{id} </delete> <insert id="addUser" parameterType="com.loger.entity.User"> insert into user(id,name,job) values (#{id},#{name},#{job}) </insert> <update id="updateUser" parameterType="com.loger.entity.User"> update user set name=#{name},job=#{job} where id=#{id} </update> </mapper>
对数据库操作的接口 UserDao.java
这个接口的方法名要与上面的 mapper 的对应 id 名一致 ,这个接口要用 mybatis 注解标识
package com.loger.dao; import java.util.List; import com.loger.annotation.MyBatisRepository; import com.loger.entity.User; @MyBatisRepository public interface UserDao { List<User> findAll(); User findById(String id); void deleteById(String id); void addUser(User user); void updateUser(User user); }
控制器 UserController.java
package com.loger.controller; import java.io.IOException; import java.util.List; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.loger.dao.UserDao; import com.loger.entity.User; @Controller @RequestMapping("/user") public class UserController { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); UserDao dao = ctx.getBean(UserDao.class); @RequestMapping("/list") public String list(HttpServletRequest request){ List<User> list = dao.findAll(); request.setAttribute("users", list); return "list"; } @RequestMapping("/add") public String add(User user){ dao.addUser(user); return "success"; } @RequestMapping("/delete") public String delete(String id){ dao.deleteById(id); return "success"; } @RequestMapping("/get") public String get(String id,HttpServletRequest request) throws IOException{ User user = dao.findById(id); request.setAttribute("user", user); return "show"; } @RequestMapping("/update") public String update(User user){ dao.updateUser(user); return "success"; } }
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> 查询所有:<br> <a href="user/list">List All</a><br><br> 新增:<br><form action="user/add"> ID:<input type="text" name="id"><br> NAME:<input type="text" name="name"><br> JOB:<input type="text" name="job"><br> <input type="submit" value="提交"> </form><br> 删除:<br><form action="user/delete"> 输入要删除的ID:<input type="text" name="id"><input type="submit" value="提交"><br> </form><br> 查询:<br><form action="user/get"> 输入要查询的ID:<input type="text" name="id"><input type="submit" value="提交"><br> </form><br> 修改:<br><form action="user/update"> 根据ID来改,不支持修改ID,输入的名字,工作为修改值:<br> ID:<input type="text" name="id"><br> NAME:<input type="text" name="name"><br> JOB:<input type="text" name="job"><br> <input type="submit" value="提交"> </form><br> </body> </html>
list.jsp (/WEB-INF/views/目录下的,包括下面几个JSP页面)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <table width="60%" border="1" cellpadding="2" cellspacing="0"> <tr> <th>ID</th> <th>NAME</th> <th>JOB</th> </tr> <c:forEach items="${users }" var="user"> <tr> <td>${user.id }</td> <td>${user.name }</td> <td>${user.job }</td> </tr> </c:forEach> </table> </body> </html>
show.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table width="60%" border="1" cellpadding="2" cellspacing="0">
<tr>
<th>ID</th>
<th>NAME</th>
<th>JOB</th>
</tr>
<tr>
<td>${user.id }</td>
<td>${user.name }</td>
<td>${user.job }</td>
</tr>
</table><br>
<a href="../index.jsp">返回</a>
</body>
</html>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> 操作成功!<br> <a href="../index.jsp">返回</a> </body> </html>
运行效果:
首页:
点击List All
增:
删:
查:
改: