创建持久层:
在src目录下创造名为com.po的包
package com.po; /** *springtest数据库中user表的持久化类,与持久层关联 */ public class MyUser { private Integer uid;//主键 private String uname; private String usex; public Integer getUid() { return uid; } public void setUid(Integer uid) { this.uid = uid; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getUsex() { return usex; } public void setUsex(String usex) { this.usex = usex; } }
创建Dao层
在src创造com.dao包,创造UserDao的接口,该接口使用@Mapper注解自动装配为MyBatis的映射接口:
package com.dao; import java.util.List; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; import com.po.MyUser; @Repository("userDao") @Mapper /*使用Spring自动扫描MyBatis的接口并装配 (Spring将指定包中所有被@Mapper注解标注的接口自动装配为MyBatis的映射接口*/ public interface UserDao { /** * 接口方法对应SQL映射文件UserMapper.xml中的id */ public List<MyUser> selectUserByUname(MyUser user); }
创建Service层:
创建com.service的包,创建UserService的接口:
package com.service; import java.util.List; import com.po.MyUser; public interface UserService { public List<MyUser> selectUserByUname(MyUser user); }
创建Conttroller层:
在src目录下创建一个名为com.controller的包,并创建UserController的控制类:
package com.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import com.dao.UserDao; import com.po.MyUser; @Controller public class UserController { @Autowired private UserDao userDao; @RequestMapping("/select") public String select(MyUser user, Model model) { List<MyUser> list = userDao.selectUserByUname(user); model.addAttribute("userList", list); return "userList"; } }
创建用户信息显示页面:
在WEB-INF创建文件夹JSP,显示userList.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> 用户信息<br> <c:forEach items="${userList}" var="user"> ${user.uid} ${user.uname} ${user.usex}<br> </c:forEach> </body> </html>
创建相关配置文件:
web.xml文件:
<?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"> <!-- 实例化ApplicationContext容器 --> <context-param> <!-- 加载src目录下的applicationContext.xml文件 --> <param-name>contextConfigLocation</param-name> <param-value> classpath:applicationContext.xml </param-value> </context-param> <!-- 指定以ContextLoaderListener方式启动Spring容器 --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!--配置DispatcherServlet --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 避免中文乱码 --> <filter> <filter-name>characterEncodingFilter</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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
springmvc-servlet.xml:
<?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.xsd"> <!-- 使用扫描机制,扫描包 --> <context:component-scan base-package="com.controller" /> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <!-- 前缀 --> <property name="prefix" value="/WEB-INF/jsp/" /> <!-- 后缀 --> <property name="suffix" value=".jsp" /> </bean> </beans>
applicationContext.xml:
<?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:tx="http://www.springframework.org/schema/tx" 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.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/springtest?characterEncoding=utf8" /> <property name="username" value="root" /> <property name="password" value="root" /> <!-- 最大连接数 --> <property name="maxTotal" value="30"/> <!-- 最大空闲连接数 --> <property name="maxIdle" value="10"/> <!-- 初始化连接数 --> <property name="initialSize" value="5"/> </bean> <!-- 添加事务支持 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 开启事务注解--> <tx:annotation-driven transaction-manager="txManager" /> <!-- 配置MyBatis工厂,同时指定数据源,并与MyBatis完美整合 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- configLocation的属性值为MyBatis的核心配置文件 --> <property name="configLocation" value="classpath:com/mybatis/mybatis-config.xml"/> </bean> <!--Mapper代理开发,使用Spring自动扫描MyBatis的接口并装配 (Spring将指定包中所有被@Mapper注解标注的接口自动装配为MyBatis的映射接口) --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- mybatis-spring组件的扫描器 --> <property name="basePackage" value="com.dao"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <!-- 指定需要扫描的包(包括子包),使注解生效。dao包在mybatis-spring组件中已经扫描,这里不再需要扫描--> <context:component-scan base-package="com.service"/> </beans>
创建MyBatis的核心配置文件和SQL映射文件:
mybatis-config.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<mappers><!-- 映射器,告诉 MyBatis到哪里去找映射文件-->
<mapper resource="com/mybatis/UserMapper.xml"/>
</mappers>
</configuration>
创建SQL映射文件UserMapper.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<mappers><!-- 映射器,告诉 MyBatis到哪里去找映射文件-->
<mapper resource="com/mybatis/UserMapper.xml"/>
</mappers>
</configuration>
创建输入页面input.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> <form action="${pageContext.request.contextPath }/select" method="post"> 输入用户名:<input type="text" name="uname"/><br> <input type="submit" value="提交"/> </form> </body> </html>