一:导入所需要的依赖与处理静态资源导出问题:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.4</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
二:在resource目录下建立mybatis.xml(其实有没有这个无所谓,这个文件的目的只是看起来像整合了mybatis层,其中mybatis.xml中的跟数据库有关的操作都交给spring去接管)
<?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>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
</configuration>
三:在resource目录下建立datebase.properties文件
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcurl=jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8
jdbc.user=root
jdbc.password=ztb
四:在resource目录下建立spring-mapper.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 https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 读取数据配置属性文件-->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!-- 配置数据源-->
<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>
<!-- 配置sqlsessionfactorybean-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 关联数据源-->
<property name="dataSource" ref="dataSource"></property>
<!--绑定mybatis核心配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- 给实体类起别名-->
<property name="typeAliasesPackage" value="com.ztb.pojo"></property>
</bean>
<!-- 注册mapper.xml文件 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ztb.mapper"></property>
</bean>
</beans>
五:在resource目录下建立spring-service.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 要有mapper层的文件-->
<import resource="spring-mapper.xml"></import>
<!--添加包扫描-->
<context:component-scan base-package="com.ztb.service"></context:component-scan>
<!--下面这些是事务操作,但是加上事务会一直报错,不知道是什么原因-->
<!--<!– 添加事务管理器–>-->
<!-- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">-->
<!--<!– 切记要配置数据源–>-->
<!-- <property name="dataSource" ref="dataSource"></property>-->
<!-- </bean>-->
<!-- <tx:advice id="myadvice" transaction-manager="transactionManager">-->
<!-- <tx:attributes>-->
<!-- <!– *指的是为所有方法添加事务 REQUIRED指的是增删改,–>-->
<!-- <tx:method name="*" propagation="NOT_SUPPORTED"/>-->
<!-- </tx:attributes>-->
<!-- </tx:advice>-->
<!--<aop:config>-->
<!-- <aop:pointcut id="mycut" expression="execution(* com.ztb.service.*.*(..))"/>-->
<!--<!– 把myadvice的事务绑定到service的所有方法中–>-->
<!-- <aop:advisor advice-ref="myadvice" pointcut-ref="mycut"></aop:advisor>-->
<!--</aop:config>-->
</beans>
六:在resource目录下建立spring-controller.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: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/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">
<!-- 要有上一层servcie的文件-->
<import resource="spring-service.xml"></import>
<!--添加包扫描-->
<context:component-scan base-package="com.ztb.controller"/>
<!-- 添加注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 如果是ajax请求,那么不需要配置视图解析器-->
<!-- 如果不是ajax请求,则需要配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
上面这几个文件分别对应ssm三层架构的几个接口与类
七:在web.xml文件中注册spring和springmvc框架,以及字符编码过滤器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--字符编码过滤问题-->
<filter>
<filter-name>character-filter</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>
</filter>
<filter-mapping>
<filter-name>character-filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 注册mvc框架-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-controller.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 注册spring框架,目的就是启动spring容器-->
<listener>
<listener-class>org.springframework.web.context.ContextCleanupListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</context-param>
</web-app>
八:建立实体类包pojo,包中建立实体类
package com.ztb.pojo;
public class User {
private Integer id;
private String name;
private String pwd;
public User() {
}
public User(Integer id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
九:建立mapper包,包中建立接口与对应的数据库的操作的xml文件,注意接口与xml文件的名字必须一致
接口:
package com.ztb.mapper;
import com.ztb.pojo.User;
import java.util.List;
public interface UserMapper {
List<User> selectAll();
int deleteid(Integer id);
int insertuser(User user);
int updateuser(User user);
}
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.ztb.mapper.UserMapper">
<select id="selectAll" resultType="user">
select * from user
</select>
<delete id="deleteid" parameterType="int">
delete from user where id=#{id}
</delete>
<insert id="insertuser" parameterType="user">
insert into user (id,name,pwd) values (#{id},#{name},#{pwd})
</insert>
<update id="updateuser" parameterType="user">
update user set pwd=#{pwd},name=#{name} where id=#{id}
</update>
</mapper>
十:建立service包,包中建立接口与对应的类
接口:
public interface UserService {
List<User> selectAll();
int deleteid(Integer id);
int insertuser(User user);
int updateuser(User user);
}
实现类:
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserMapper userMapper;
public List<User> selectAll() {
return userMapper.selectAll();
}
public int deleteid(Integer id) {
return userMapper.deleteid(id);
}
public int insertuser(User user) {
return userMapper.insertuser(user);
}
public int updateuser(User user) {
return userMapper.updateuser(user);
}
}
十一:进行测试,测试通过后在进行controller层
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath:spring-service.xml"})
public class ServiceTest {
@Autowired
UserService userService;
@Test
public void test1(){
List<User> list = userService.selectAll();
for (User user : list) {
System.out.println(user);
}
}
@Test
public void test2(){
int deleteid = userService.deleteid(16);
System.out.println(deleteid);
}
@Test
public void test3(){
int deleteid = userService.insertuser(new User(14,"宋","www"));
System.out.println(deleteid);
}
}
十二:进行controller层的编写:
@Controller
public class UserController {
@Autowired
UserService userService;
@RequestMapping("showuser")
public String showuser(HttpServletRequest request){
List<User> list = userService.selectAll();
request.setAttribute("userlist",list);
return "main";
}
十三:配置tomcat,测试浏览器与服务器的交互
遇到的一些问题以及解决方法:
一:spring单元测试的依赖版本要和spring-*各个依赖的版本相同,否则会报错
二:添加包扫描时,要用<context:compoent-scan>,不能用<context:scan>,否则会报无法找到bean的错。
三:在spring-service.xml文件中一定要导入spring-mapper.xml,在在spring-controller.xml文件中一定要导入spring-service.xml,否则会报错
四:需要在web-inf目录下新建lib包,然后加入全部的外部依赖,步骤file-project settings-artifacts
五:就是事务的切面配置,只要配置事务切面,就会报错,不知道该怎么解决
标签:www,http,spring,springframework,ssm,整合,org,schema From: https://www.cnblogs.com/zhangtaibing/p/16612374.html