搭建框架
1. 使用spring整合mybatis,springmvc:引入支持,依赖jar包;配置数据库连接信息db.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=UTF-8
username=root
password=mjsw
2. 创建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>
<!-- mybatis:连接数据库,由spring进行统一管理 -->
</configuration>
3. 创建applicationContext.xml;spring整合mybatis
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
">
<!-- 配置Service层扫描包 -->
<context:component-scan base-package="com.dao"></context:component-scan>
<context:component-scan base-package="com.service"></context:component-scan>
<!-- 引入连接信息 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 配置数据源:数据库连接信息 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driverClassName}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</bean>
<!-- 配置sqlSessionFactory:mybatis实现对数据库操作 -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 设置数据源参考:加载数据库连接信息 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 扫描数据库接口映射文件mapper.xml -->
<property name="mapperLocations" value="classpath:com/mapper/*.xml"></property>
<!-- 引入mybatis配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<!-- DAO层操作数据库接口所在包 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.dao"></property>
<!-- 指定该包下接口操作对应的SqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"></property>
</bean>
<!-- 注入事务管理类bean -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事务通知属性:由指定bean进行事务管理 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 定义事务传播属性 -->
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="REQUIRED" read-only="true"/>
<tx:method name="*" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置事务的切入点 -->
<aop:config>
<!-- 指定包下任意方法进行事务管理 -->
<aop:pointcut expression="execution(* com.service.*.*(..))" id="servicePoint"/>
<!-- 事务与方法建立联系 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="servicePoint"/>
</aop:config>
</beans>
4. 创建springmvc.xml,整合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: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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
">
<!-- 配置扫描包 -->
<context:component-scan base-package="com.controller"/>
<!-- 配置注解驱动:可以不用配置处理器映射器和处理器适配器 -->
<mvc:annotation-driven/>
<!-- 配置视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
5. 配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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_3_0.xsd" version="3.0">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 前端控制器 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- spring容器监听器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 中文乱码过滤器 -->
<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>
测试
package com.model;
/*
* 实体类:与数据库表对应
*/
public class Student {
private Integer stuno = null;
private String sname;
private int age;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(Integer stuno, String sname, int age) {
super();
this.stuno = stuno;
this.sname = sname;
this.age = age;
}
public Integer getStuno() {
return stuno;
}
public void setStuno(Integer stuno) {
this.stuno = stuno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [stuno=" + stuno + ", sname=" + sname + ", age=" + age
+ "]";
}
}
package com.dao;
import com.model.Student;
/*
* dao层处理数据库接口
*/
public interface StudentDao {
public int insert(Student stu);
}
<?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.dao.StudentDao">
<!-- 映射文件 -->
<insert id="insert" parameterType="com.model.Student">
insert into student values(null,#{sname},#{age})
</insert>
</mapper>
package com.service;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.dao.StudentDao;
import com.model.Student;
/*
* 业务层:加工dao层数据
*/
// 使用注解指明service层
@Service
public class StudentService {
// 使用Resource注解,替代在spring容器中注入javabean
@Resource
private StudentDao studentDao;
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
public boolean insert(Student stu){
int i = studentDao.insert(stu);
if(i>0){
return true;
}
return false;
}
}
package com.controller;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.model.Student;
import com.service.StudentService;
/*
* 控制层:访问该路径可以将学生信息写入数据库
*/
@Controller
@RequestMapping("/stu")
public class StudentController {
@Resource
private StudentService studentService;
public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}
@RequestMapping("/insert.do")
public String insert(){
Student stu = new Student(null, "墨渐生微", 19);
boolean flag = studentService.insert(stu);
System.out.println(flag);
return "index";
}
}