完成简单的表单注册功能
- 定义实体类和对应的数据表
- Student实体类
- Student表
- Student实体类
- 定义页面和处理器
- index页面
<html> <body> <form action="test/register.do" method="POST"> 姓名:<input type="text" name="name"/><br> 年龄:<input type="text" name="age"/><br> <input type="submit" value="注册"/> </form> </body> </html>
- 处理器(实现Controller接口)
package com.enpong.web; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; import com.enpong.beans.Student; import com.enpong.service.IStudentService; public class StudentController implements Controller { private IStudentService studentService; public void setStudentService(IStudentService studentService) { this.studentService = studentService; } @Override public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { //接收请求参数 String name = request.getParameter("name"); String ageStr = request.getParameter("age"); Integer age = Integer.valueOf(ageStr); Student student = new Student(name, age); studentService.addStudent(student); return new ModelAndView("/WEB-INF/jsp/welcome.jsp"); } }
- index页面
- 定义Service
- 定义业务接口
package com.enpong.service; import com.enpong.beans.Student; public interface IStudentService { void addStudent(Student student); }
- 实现类
package com.enpong.service.impl; import com.enpong.beans.Student; import com.enpong.dao.IStudentDao; import com.enpong.service.IStudentService; public class StudentServiceImpl implements IStudentService { private IStudentDao studentDao; public void setStudentDao(IStudentDao studentDao) { this.studentDao = studentDao; } @Override public void addStudent(Student student) { studentDao.insertStudent(student); } }
- 定义业务接口
- 定义Dao接口,mapper配置文件
- Dao接口
package com.enpong.dao; import com.enpong.beans.Student; public interface IStudentDao { void insertStudent(Student stu); }
- 在Dao包中定义MyBatis的映射文件IStudentDao.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.enpong.dao.IStudentDao"> <insert id="insertStudent"> insert into student(name,age) value(#{name}, #{age}) </insert> </mapper>
- Dao接口
- 定义jdbc.properties
- 在类路径下的resources目录中定义
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://172.31.71.207:3306/test jdbc.user= jdbc.password=
- 在类路径下的resources目录中定义
- 定义MyBatis的主配置文件
- 在类路径下的resources目录中定义,该配置文件中主要注册Mapper中所使用的类的别名及Mapper的位置
<?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> <!-- 类的别名 --> <typeAliases> <package name="com.enpong.beans"/> </typeAliases> <!-- 映射文件位置 --> <mappers> <package name="com.enpong.dao"/> </mappers> </configuration>
- 在类路径下的resources目录中定义,该配置文件中主要注册Mapper中所使用的类的别名及Mapper的位置
- 定义spring的配置文件
- spring的配置文件均在resources目录中定义
- spring-db.xml,该配置文件中用于注册数据源相关bea n
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 注册数据源,C3P0 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- 注册jdbc属性文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> </beans>
- spring-mybatis.xml,主要用于注册SqlSessionFactoryBean及Mapper动态代理生成器的bean
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 注册SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- 指定mybatis.xml文件位置 --> <property name="configLocation" value="classpath:mybatis.xml"></property> </bean> <!-- 注册Mapper扫描配置器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> <!-- 指定Dao接口扫描包 --> <property name="basePackage" value="com.enpong.dao"></property> </bean> </beans>
- spring-service.xml,用于进行Service的bean的注册,这里注入Dao属性时用的是Mapper映射文件的名字
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <bean id="studentService" class="com.enpong.service.impl.StudentServiceImpl"> <property name="studentDao" ref="IStudentDao"></property> </bean> </beans>
- spring-tx.xml,用于注册事务相关bean
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 注册事务通知 --> <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 连接点 --> <tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!-- aop配置 切面--> <aop:config> <!-- 切入点 --> <aop:pointcut expression="execution(* *..service.*.*(..))" id="myPointcut"/> <!-- 顾问 --> <aop:advisor advice-ref="transactionAdvice" pointcut-ref="myPointcut"/> </aop:config> </beans>
- spring-mvc.xml,用于注册SpringMVC的处理器,也可能需要配置处理器映射器、处理器适配器、视图解析器等与中央调度器(Dispatcher)有关的Bean
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="/test/register.do" class="com.enpong.web.StudentController"> <property name="studentService" ref="studentService"></property> </bean> </beans>