ssm的环境搭建

项目架构
在这里插入图片描述
jar包:

spring-core-3.2.3.RELEASE.jar     spring核心
	spring-webmvc-3.2.3.RELEASE.jar    springmvc
	spring-web-3.2.3.RELEASE.jar       springmvc
	spring-jdbc-3.2.3.RELEASE.jar      spring-jdbc连接数据库  
	spring-context-3.2.3.RELEASE.jar   spring-context上下文环境
	spring-beans-3.2.3.RELEASE.jar     核心
	spring-context-support-3.2.3.RELEASE.jar  上下文支持
	spring-tx-3.2.3.RELEASE.jar           transaction 事务
	spring-aop-3.2.3.RELEASE.jar          切面
	aopalliance-1.0.jar                   织入
	spring-expression-3.2.3.RELEASE.jar   表达式
	mysql-connector-java-5.1.0-bin.jar    mysql
	mybatis-3.4.4.jar                     mybatis核心
	mybatis-spring-1.0.2.jar              mybatis和spring结合
	commons-dbcp.jar                       
	commons-pool.jar
	commons-logging-1.1.1.jar
	log4j-1.2.16.jar

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.teng</groupId>
  <artifactId>ssm01</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>ssm01 Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.0.8.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.9.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.9.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
    <dependency>
      <groupId>aopalliance</groupId>
      <artifactId>aopalliance</artifactId>
      <version>1.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>1.4</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.46</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.0.8.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.25</version>
    </dependency>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.12</version>
    </dependency>

    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.10.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.47</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.3</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>aliyun-java-sdk-core</artifactId>
      <version>4.0.6</version>
    </dependency>

    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
      <version>1.1.0</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/com.oracle/ojdbc6 -->
    <dependency>
      <groupId>com.oracle</groupId>
      <artifactId>ojdbc6</artifactId>
      <version>11.1.0.6.0</version>
      <scope>system</scope>
      <systemPath>${basedir}/web/WEB-INF/lib/ojdbc6-11.1.0.6.0.jar</systemPath>
    </dependency>

    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.5</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.1</version>
    </dependency>
    
  </dependencies>
  <build>
    <finalName>ssm01</finalName>
  </build>
</project>

db.properties

mysql.driverClassName=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/java1807
mysql.username=root
mysql.password=123456
mysql.maxActive=100
mysql.initialSize=5

oracle.driverClassName=oracle.jdbc.OracleDriver
oracle.url=jdbc:oracle:thin:@localhost:1521:orcl
oracle.username=jtf
oracle.password=123456
oracle.maxActive=100
oracle.initialSize=5

spring.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: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.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--开启注解扫描功能-->
    <context:annotation-config></context:annotation-config>
    <!--定义注解扫描的包-->
    <context:component-scan base-package="com.teng.mapper"></context:component-scan>
    <context:component-scan base-package="com.teng.service"></context:component-scan>
    <context:component-scan base-package="com.teng.domain"></context:component-scan>
    <!--开启aop自动代理模式-->
    <aop:aspectj-autoproxy proxy-target-class="true"/>

    <!--从上下文环境中读取db.properties配置文件-->
    <context:property-placeholder location="classpath*:db.properties"></context:property-placeholder>

    <bean id="dataSource"  class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${oracle.driverClassName}"></property>
        <property name="url" value="${oracle.url}"></property>
        <property name="username" value="${oracle.username}"></property>
        <property name="password" value="${oracle.password}"></property>
        <property name="maxActive" value="${oracle.maxActive}"></property>
        <property name="initialSize" value="${oracle.initialSize}"></property>
    </bean>

    <!--通过mybatis和spring结合的类,创建sqlsessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!--指定myabtis配置文件位置-->
        <property name="configLocation" value="classpath:mybatis-config.xml" />
    </bean>
    <!-- scan for mappers and let them be autowired
    MapperScannerConfigurer Mybatis-Spring 会自动为我们注册Mapper对应的MapperFactoryBean对象-->
    <!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.teng.mapper" />
    </bean>
    <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 配置事务属性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*"    propagation="REQUIRED"/>
            <tx:method name="update*"  propagation="REQUIRED"/>
            <tx:method name="delete*"  propagation="REQUIRED"/>
            <tx:method name="*" propagation="NOT_SUPPORTED"></tx:method>
        </tx:attributes>
    </tx:advice>

    <!-- 配置事务切入点 -->
    <aop:config>
        <aop:pointcut expression="execution(* com.teng.service.impl.*.*(..))" id="pointCut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/>
    </aop:config>

</beans>

springmvc-servlet.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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       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
		        http://www.springframework.org/schema/context/spring-context.xsd
		        http://www.springframework.org/schema/mvc
		        http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--指定视图的前缀和后缀,Controller返回的String类型与这里的前后缀拼接,构成返回的视图页面地址-->
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--开启注解扫描功能-->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!-- 处理请求返回json字符串的中文乱码问题 -->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <!-- 解决Controller返回json中文乱码问题 -->
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <!-- <property name="supportedMediaTypes" value="text/html;charset=UTF-8" /> -->
                <!-- <property name="supportedMediaTypes" value="application/json;charset=UTF-8" > -->
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html;charset=UTF-8</value>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
            </bean>

            <!-- fastJson配置 -->
            <bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/json;charset=UTF-8</value>
                        <value>text/html;charset=UTF-8</value>
                    </list>
                </property>
            </bean>

        </mvc:message-converters>
    </mvc:annotation-driven>


    <!--定义注解扫描的包-->
    <context:component-scan base-package="com.teng.controller"></context:component-scan>

    <!--处理静态资源-->
    <mvc:default-servlet-handler></mvc:default-servlet-handler>

    <!--将链接中的静态的访问路径映射为URL,常用于加载html、js、css、图片、视频等静态资源-->
    <!-- <mvc:resources mapping="/js/**" location="/background/js/"/>
     <mvc:resources mapping="/images/**" location="/background/images/"/>
     <mvc:resources mapping="/css/**" location="/background/css/"/>
     <mvc:resources mapping="/fonts/**" location="/background/fonts/"/>
     <mvc:resources mapping="/lib/**" location="/background/lib/"/>-->

    <!--登录拦截器-->
    <!-- <mvc:interceptors>
         <mvc:interceptor>
             &lt;!&ndash; 拦截所有URL中包含/user/的请求 &ndash;&gt;
             <mvc:mapping path="/**"/>
             <mvc:exclude-mapping path="/**/fonts/**"/>
             <mvc:exclude-mapping path="/**/css/**"/>
             <mvc:exclude-mapping path="/**/js/**"/>
             <mvc:exclude-mapping path="/**/images/**"/>
             <mvc:exclude-mapping path="/**/lib/**"/>
             <mvc:exclude-mapping path="/**/login.html"/>
             &lt;!&ndash;<mvc:exclude-mapping path="/**/index.html"/>&ndash;&gt;
             <mvc:exclude-mapping path="/user/toLogin"/>
             <bean class="com.qy.controller.LoginInterceptor"></bean>
         </mvc:interceptor>
     </mvc:interceptors>
 -->
    <!--springmvc定义好的,用来处理上传文件的类-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 设置上传文件的最大尺寸为1MB -->
        <property name="maxUploadSize">
            <value>1048576</value>
        </property>
    </bean>

</beans>

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>

    <settings>
        <!--是否使用缓存 开发中禁用-->
        <setting name="cacheEnabled" value="false"/>
        <!--下划线和驼峰式命名法的匹配  -->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <!--使用启用懒加载机制  true启用-->
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="logImpl" value="LOG4J2"></setting>
        <setting name="multipleResultSetsEnabled" value="true"/>
        <setting name="useColumnLabel" value="true"/>
        <setting name="useGeneratedKeys" value="true"/>
        <setting name="autoMappingBehavior" value="PARTIAL"/>
        <setting name="autoMappingUnknownColumnBehavior" value="WARNING"/>
        <setting name="defaultExecutorType" value="SIMPLE"/>
        <setting name="defaultStatementTimeout" value="25"/>
        <setting name="defaultFetchSize" value="100"/>
        <setting name="safeRowBoundsEnabled" value="false"/>
        <setting name="localCacheScope" value="SESSION"/>
        <setting name="jdbcTypeForNull" value="OTHER"/>
        <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>
    </settings>

    <typeAliases>
        <!--给类起别名-->
        <!--<typeAlias type="com.qy.domain.Question" alias="Question"></typeAlias>-->
        <!--给包中所有的类起别名 默认名字为类名 -->
        <package name="com.teng.domain"></package>
    </typeAliases>

    <mappers>
        <!--引入mapper映射文件的位置-->
       <!--  <mapper resource="com/teng/mapper/stuMapper.xml"/>
         <mapper resource="com/teng/mapper/TeacherMapper.xml"/>-->
    </mappers>


</configuration>

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="trace">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

web.xml

<?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_3_1.xsd"
         version="3.1">

    <!--filter 过滤器 用来解决中文乱码问题-->
    <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>
    </filter>
    <filter-mapping>
        <filter-name>characterEncoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <servlet>
        <servlet-name>default1</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>default1</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--配置spring-->
    <!-- 配置去哪裡加載spring的配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring.xml</param-value>
    </context-param>
    <!-- 配置spring的監聽器,加載資源初始化文件 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

实体类
Teacher

package com.teng.domain;

import java.util.List;

public class Teacher {

    private Integer id;
    private String name;
    private String className;
    private List<Stu> stus;

    public List<Stu> getStus() {
        return stus;
    }

    public void setStus(List<Stu> stus) {
        this.stus = stus;
    }

    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 getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }
}

接口mapper
TeacherMapper

package com.teng.mapper;

import com.teng.domain.Teacher;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface TeacherMapper {
    public List<Teacher> selectAllTeacher();
    public List<Teacher> findAllTeacher();
}

对象映射文件
TeacherMapper.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 命名空间 唯一的-->
<mapper namespace="com.teng.mapper.TeacherMapper">
    <!--一对多的第一种写法,一般考虑到性能,不会这样实现-->
    <resultMap id="TeacherMap" type="Teacher" autoMapping="true">
    <collection property="stus" ofType="Stu" column="id" autoMapping="true">
    </collection>
    </resultMap>
    <!--查询所有的老师及各自的所有学生,第一种形式 一一对应-->
    <select id="selectAllTeacher" parameterType="Teacher" resultMap="TeacherMap">
        select t.id,t.name,t.class_name,
        s.id as sid,s.name as sname,s.class_name as className
        from
        teacher t join stu s
        on t.id = s.teacher_id
    </select>
    <resultMap type="Teacher" id="teacherMaps" autoMapping="true">
        <collection property="stus" ofType="Stu" select="getStudents" column="id">
        </collection>
    </resultMap>
    <!-- 查询所有的老师级各自的所有学生,一对多关联-->
    <select id="findAllTeacher" parameterType="Teacher" resultMap="teacherMaps">
		SELECT
			t.id,
			t.name,
			t.class_name
		FROM
			teacher t
	</select>
    <select id="getStudents" parameterType="int" resultType="Stu">
		select
			s.id,
			s.name,
			s.class_name as className
		from stu s
		where teacher_id = #{id}
	</select>
</mapper>


service层
TeacherService

package com.teng.service;

import com.teng.domain.Teacher;

import java.util.List;

public interface TeacherService {
    public List<Teacher> selectAllTeacher();
    public List<Teacher> findAllTeacher();
}

TeacherServiceImpl

package com.teng.service.impl;

import com.teng.domain.Teacher;
import com.teng.mapper.TeacherMapper;
import com.teng.service.TeacherService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {
    @Resource
    private TeacherMapper teacherMapper;

    public List<Teacher> selectAllTeacher() {
        return null;
    }

    public List<Teacher> findAllTeacher() {
        return teacherMapper.findAllTeacher();
    }

    public void setTeacherMapper(TeacherMapper teacherMapper) {
        this.teacherMapper = teacherMapper;
    }
}

controller层
TeacherController

package com.teng.controller;

import com.alibaba.fastjson.JSON;
import com.teng.service.TeacherService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class TeacherController {

    @Resource
    private TeacherService teacherService;

    @RequestMapping("/showTeacher")
    @ResponseBody
    public String showTeacher(){
        List list = teacherService.findAllTeacher();
        return JSON.toJSONString(list);
    }

    public void setTeacherService(TeacherService teacherService) {
        this.teacherService = teacherService;
    }
}

测试结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值