Version:annotation版java
文檔結構圖:web
從底層開始作起:spring
<?xml version="1.0" encoding="UTF-8"?>sql
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">session
<display-name></display-name>mybatis
<!-- 配置spring裝載bean的設置 -->mvc
<context-param>app
<param-name>contextConfigLocation</param-name>jsp
<param-value>classpath:applicationcontext.xml</param-value>post
</context-param>
<!-- 配置編碼 -->
<filter>
<filter-name>CharacterEncoding</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>CharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置springmvc須要的組件設置 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
02.配置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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
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/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd ">
<!-- 配置 註解start -->
<!--01. 包掃描器 -->
<context:component-scan base-package="cn.zym"></context:component-scan>
<!-- 02.數據源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 1.1 關聯jdbc.properties -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 02.配置SessionFactory -->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--03. dao -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sessionFactory"></property>
<property name="basePackage" value="cn.zym.dao"></property>
</bean>
<!-- 開啓事務 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置註解驅動 是基於註解的方式使用事務配置聲明(eg:@Transactional) -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
<!-- 配置 註解end -->
通常使用Mybatis時不建議使用註解(會下降程序效率和增長開發難度):這裏依然仍是選擇原生的配置;
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>
<typeAliases>
這裏將pojo包下的類設置了別名(在XXXdao.xml中直接調用該包下的類名便可)
<package name="cn.zym.pojo"/>
</typeAliases>
。。。。添加其餘配置文件
</configuration>
Dao層書寫:
Ok,這裏須要添加對應dao的Mybatis操做文件,該文件經過spring容器生成了代理類,在上面有提到;
<!--03. dao --> 該代理類肯能會有多個,每一個代理類的名稱的生成規則:
Interface:IUserDao proxy:IUserDao
Interface:UserDao proxy:userDao
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sessionFactory"></property>
<property name="basePackage" value="cn.zym.dao"></property>
</bean>
Ok,配置dao對應的文件
IUserDao.xml 這裏的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="cn.zym.dao.IUserDao">
<select id="save" parameterType="User">
insert into user (name,password) values (#{name},#{password})
</select>
</mapper>
Ok,接下來是註解配置Controller:
@Controller
@RequestMapping("/")
public class UserController{
@Autowired
@Qualifier("userservice")
private IUserService service;
@RequestMapping(value="/adduser.do",method=RequestMethod.POST)
public String addUser(HttpServletRequest request,User user) throws UnsupportedEncodingException{
request.setCharacterEncoding("utf-8");
System.out.println(user);
service.save(user);
return "forward:/welcome.jsp";
}
這樣在前臺直接請求攜帶數據的時候將會觸發該Handler,將數據報錯到DB中
Useradd.jsp;
<body>
<form action="adduser.do" method="post">
<input type="text" name="name"/><br/>
<input type="text" name="password"/><br/>
<input type="submit" value="submit"/>
</form>
</body>