1 创建war类型maven项目并加载web配置文件,导入对应库
<properties>
<spring.version>4.3.18.RELEASE</spring.version>
<mybatis.version>3.4.6</mybatis.version>
<mysql.version>5.1.38</mysql.version>
<springmybatis.version>2.0.1</springmybatis.version>
<druid.version>1.1.16</druid.version>
<junit.version>4.12</junit.version>
<aspectj.version>1.9.4</aspectj.version>
<jspapi.version>2.2</jspapi.version>
<servletapi.version>4.0.1</servletapi.version>
<jstl.version>1.2</jstl.version>
</properties>
<!--导入依赖库-->
<dependencies>
<!--spring 基本库 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 数据连接部分 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- mybatis和spring-mybatis部分 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${springmybatis.version}</version>
</dependency>
<!-- spring 事务处理库和spring aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring测试库 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- springmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servletapi.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>${jspapi.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
</dependencies>
<!-- 运行web项目的tomcat插件 -->
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>9090</port>
<path>/</path>
<uriEncoding>UTF-8</uriEncoding>
<!-- <finalName>mgr</finalName> -->
<server>tomcat7</server>
</configuration>
</plugin>
</plugins>
</build>
2 创建对应表的实体类
package com.demo.po;
//javaBean
public class Tab_topic {
private Integer tid;
private String tname;
private String bak;
public Integer getTid() {
return tid;
}
public void setTid(Integer tid) {
this.tid = tid;
}
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
public String getBak() {
return bak;
}
public void setBak(String bak) {
this.bak = bak;
}
}
3 编写连接数据库的属性文件 src/main/resources/db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/smartdb?characterEncoding=utf-8
jdbc.user=root
jdbc.password=root
4 编写spring-mybatis配置 src/main/resources/spring-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 00 通过上下引用自定义属性文件 -->
<context:property-placeholder
location="db.properties" />
<!-- 01 配置druid连接池对象 -->
<bean id="ds"
class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
<property name="filters" value="stat" />
<property name="maxActive" value="20" />
<property name="initialSize" value="1" />
<property name="maxWait" value="60000" />
<property name="minIdle" value="1" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="validationQuery" value="SELECT 'x'" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize"
value="50" />
</bean>
<!-- 02 spring mybatis结合的SqlSessionFactoryBean -->
<bean id="sf" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="ds"></property>
<!-- mapperLocations属性对应mybatis的操作映射配置文件路径 -->
<property name="mapperLocations" value="classpath:com/demo/mapper/*.xml"></property>
<!-- 通过typeAliasesPackage属性配置实体类对应包名 -->
<property name="typeAliasesPackage" value="com.demo.po"></property>
</bean>
<!-- 03 配置mybatis操作映射接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.demo.dao" />
<property name="sqlSessionFactoryBeanName" value="sf" />
</bean>
</beans>
5 编写mybatis操作接口 com.demo.dao.ITopicDao.java
package com.demo.dao;
import java.util.List;
import com.demo.po.Tab_topic;
public interface ITopicDao {
public int update(Tab_topic po);
public List<Tab_topic> findall();
}
6 编写mybatis接口对应操作映射文件 com/demo/mapper/topic.mapper.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设置操作接口的全路径
public interface ITopicDao {
public int update(Tab_topic po);
public List<Tab_topic> findall();
}
-->
<mapper namespace="com.demo.dao.ITopicDao">
<update id="update" parameterType="tab_topic">
update tab_topic set tname=#{tname} where tid=#{tid}
</update>
<select id="findall" resultType="tab_topic">
select * from tab_topic
</select>
</mapper>
7 编写对应业务和事务管理配置文件 src/main/resouces/spring-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 扫描所有的业务bean所在的路径 -->
<context:component-scan base-package="com.demo.service"></context:component-scan>
</beans>
稍后加上事务处理部分
8 编写业务bean
package com.demo.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.demo.dao.ITopicDao;
import com.demo.po.Tab_topic;
@Service
public class TopicService {
// 注入dao对象
@Autowired
private ITopicDao dao;
public int update(Tab_topic po) {
return dao.update(po);
}
public List<Tab_topic> findall(){
return dao.findall();
}
}
9 编写测试业务的类 src/test/java/MTest.java
package com.demo;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.demo.po.Tab_topic;
import com.demo.service.TopicService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring-dao.xml","classpath:spring-service.xml"})
public class MTest {
// 注入测试的业务bean
@Autowired
private TopicService topser;
@Test
public void update() {
Tab_topic po=new Tab_topic();
po.setTid(3);
po.setTname("女裙");
topser.update(po);
}
@Test
public void query() {
List<Tab_topic> li=topser.findall();
for (Tab_topic t : li) {
System.out.println(t.getTid()+","+t.getTname());
}
}
}
10 在web项目中创建spring-mvc的配置文件, src/main/resources/springweb.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 1 配置扫描自定义控制器所在包 -->
<context:component-scan base-package="com.demo.ctl"></context:component-scan>
<!-- 2 当前web项目支持springmvc注解方式的配置 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 3 针对静态资源如 图片,html页面,js,css等 不执行springmvc -->
<mvc:default-servlet-handler/>
<!-- 4 配置在springmvc中调整资源规范
InternalResourceViewResolver对象针对springmvc跳转功能,
通过prefix属性,设置统一的页面路径;
通过suffix属性,识别页面资源的扩展名
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pager/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
11 在web.xml文件配置spring-mvc框架
<!-- 00 为springweb监听器提供配置位置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</context-param>
<!-- 01 通过springweb提供监听器 加载 springweb的上下文 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 02 在web启动中配置spring mvc DispatcherServlet(springmvc前端控制器)-->
<servlet>
<servlet-name>xx</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springweb.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>xx</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
网友评论