搭建SSM(SpringMVC+Spring+Mybatis)架构

1.导入jar包

 <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</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>

2.手动导入Oracle的jar包

3.完成resources目录下xml文件的配置

db.properties

mysql.driverClassName=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/forage?useSSL=true
mysql.username=root
mysql.password=970817
mysql.maxIdle=100
mysql.initialSize=5

oracle.driverClassName=oracle.jdbc.OracleDriver
oracle.url=jdbc:oracle:thin:@localhost:1521:orcl
oracle.username=scott
oracle.password=tiger
oracle.maxIdle=100
oracle.initialSize=5

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>
        <Logger name="com.qy.mapper" level="trace" additivity="false">
            <AppenderRef ref="Console"/>
        </Logger>

    </Loggers>
</Configuration>

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="lazyLoadingEnabled" value="true"/>
        <!--mybatis日志-->
        <setting name="logImpl" value="LOG4J2"/>
        <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="mapUnderscoreToCamelCase" value="true"/>
        <setting name="localCacheScope" value="SESSION"/>
        <setting name="jdbcTypeForNull" value="OTHER"/>
        <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>
    </settings>

    <typeAliases>
        <!--给类起别名-->
        <!--<typeAlias alias="User" type="com.jie.domain.User"/>-->
        <!--<typeAlias alias="Breed" type="com.jie.domain.Breed"/>-->
       <!--给包中所有的类起别名,默认为类名-->
        <package name="com.jie.domain"></package>
    </typeAliases>

</configuration>

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:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mc="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/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
		        http://www.springframework.org/schema/mvc
		        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--开启注解扫描功能-->
    <context:annotation-config></context:annotation-config>

    <!--定义注解扫描的包-->
    <context:component-scan base-package="com.jie.domain"></context:component-scan>
    <context:component-scan base-package="com.jie.mapper"></context:component-scan>
    <context:component-scan base-package="com.jie.service"></context:component-scan>

    <!--开启aop自动代理-->
    <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
    <!--从上下文中读取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="maxIdle" value="${oracle.maxIdle}"></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.jie.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="add*" propagation="REQUIRED"/>
            <tx:method name="update" propagation="REQUIRED"/>
            <tx:method name="delete" propagation="REQUIRED"/>
            <tx:method name="*" propagation="NOT_SUPPORTED"/>
        </tx:attributes>
    </tx:advice>
    <!-- 配置事务切入点 -->
    <aop:config>
        <aop:pointcut expression="execution(* com.jie.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"
       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">



    <!--视图解析器-->
    <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">
                    <list>
                        <value>text/html;charset=UTF-8</value>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
            </bean>

           <!-- &lt;!&ndash; fastJson配置 &ndash;&gt;-->
            <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.jie.controller"></context:component-scan>

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

    <!--将链接中的静态的访问路径映射为URL,常用于加载html、js、css、图片、视频等静态资源-->
    <!--<mvc:resources mapping="/js/**" location="/forward/AmazeUI-2.4.2/"/>-->
    <!--<mvc:resources mapping="/images/**" location="/forward/basic/"/>-->
    <!--<mvc:resources mapping="/js/**" location="/forward/js/"/>-->
    <!--<mvc:resources mapping="/images/**" location="/forward/images/"/>-->
    <!--<mvc:resources mapping="/js/**" location="/forward/home/**"/>-->
    <!--<mvc:resources mapping="/images/**" location="/forward/person/"/>-->
    <!--<mvc:resources mapping="/css/**" location="/forward/css/"/>-->
    <!--<mvc:resources mapping="/js/**" location="/background/lib/"/>-->
    <!--<mvc:resources mapping="/images/**" location="/background/static/"/>-->
    <!--<mvc:resources mapping="/css/**" location="/background/temp/"/>-->
    <!--<mvc:resources mapping="/myjs/**" location="/background/myjs/**"/>-->
    <!--<mvc:resources mapping="/myjs/**" location="/forward/myjs/**"/>-->
    <!--登录拦截器-->
    <!--<mvc:interceptors>-->
        <!--<mvc:interceptor>-->
            <!--&lt;!&ndash; 拦截所有URL中包含/user/的请求 &ndash;&gt;-->
            <!--<mvc:mapping path="/**"/>-->
            <!--<mvc:exclude-mapping path="/background/lib/**"/>-->
            <!--<mvc:exclude-mapping path="/background/static/**"/>-->
            <!--<mvc:exclude-mapping path="/background/temp/**"/>-->
            <!--<mvc:exclude-mapping path="/background/login.html"/>-->
            <!--<mvc:exclude-mapping path="/forward/images/**"/>-->
            <!--<mvc:exclude-mapping path="/forward/AmazeUI-2.4.2/**"/>-->
            <!--<mvc:exclude-mapping path="/forward/basic/**"/>-->
            <!--<mvc:exclude-mapping path="/forward/css/**"/>-->
            <!--<mvc:exclude-mapping path="/forward/js/**"/>-->
            <!--<mvc:exclude-mapping path="/forward/frame.html"/>-->
            <!--<mvc:exclude-mapping path="/forward/home/home3.html"/>-->
            <!--<mvc:exclude-mapping path="/forward/home/introduction.html"/>-->
            <!--<mvc:exclude-mapping path="/forward/.project"/>-->
            <!--<mvc:exclude-mapping path="/**/login.html"/>-->
            <!--<mvc:exclude-mapping path="/**/register.html"/>-->
            <!--<mvc:exclude-mapping path="/**/toLogin"/>-->
            <!--<mvc:exclude-mapping path="/user/toRegister"/>-->
            <!--<mvc:exclude-mapping path="/**/myjs/**"/>-->
            <!--<mvc:exclude-mapping path="/forward/images/**"/>-->
            <!--<mvc:exclude-mapping path="/valid/**"/>-->
            <!--<mvc:exclude-mapping path="/commodity/**"/>-->
            <!--<mvc:exclude-mapping path="/evaluate/toShow"/>-->
            <!--<mvc:exclude-mapping path="/round/**"/>-->
            <!--<mvc:exclude-mapping path="http://sandbox.runjs.cn/uploads/rs/267/g3ugugjp/area.js"/>-->
            <!--<bean class="com.jie.controller.LoginInterceptor"/>-->
        <!--</mvc:interceptor>-->
    <!--</mvc:interceptors>-->
    <!--springmvc定义好的,用来处理上传文件的类-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 设置上传文件的最大尺寸为10MB -->
        <property name="maxUploadSize">
            <value>10485760</value>
        </property>
    </bean>

 </beans>

3.测试

这部分和SSH几乎一样

实体类

package com.jie.domain;

import java.util.List;

public class Dept {
    private int deptno;
    private String dname;
    private String loc;
    //一对多
    private List<Emp>empList;

    public List<Emp> getEmpList() {
        return empList;
    }

    public void setEmpList(List<Emp> empList) {
        this.empList = empList;
    }

    public int getDeptno() {
        return deptno;
    }

    public void setDeptno(int deptno) {
        this.deptno = deptno;
    }

    public String getDname() {
        return dname;
    }

    public void setDname(String dname) {
        this.dname = dname;
    }

    public String getLoc() {
        return loc;
    }

    public void setLoc(String loc) {
        this.loc = loc;
    }
}
package com.jie.domain;

import java.sql.Date;

public class Emp {
    private int empno;
    private String ename;
    private String job;
    private int mgr;
    private Date hiredate;
    private double sal;
    private double comm;
    private int deptno;
    //多对一关联
    private Dept dept;

    public int getEmpno() {
        return empno;
    }

    public void setEmpno(int empno) {
        this.empno = empno;
    }

    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public int getMgr() {
        return mgr;
    }

    public void setMgr(int mgr) {
        this.mgr = mgr;
    }

    public Date getHiredate() {
        return hiredate;
    }

    public void setHiredate(Date hiredate) {
        this.hiredate = hiredate;
    }

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    }

    public double getComm() {
        return comm;
    }

    public void setComm(double comm) {
        this.comm = comm;
    }

    public int getDeptno() {
        return deptno;
    }

    public void setDeptno(int deptno) {
        this.deptno = deptno;
    }

    public Dept getDept() {
        return dept;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }
}

mapper

@Repository这个注解放在了mapper的接口上
package com.jie.mapper;

import com.jie.domain.Dept;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository(value = "deptMapper")
public interface DeptMapper {
    //查询一个
    public Dept selectBreed(int id);
    //查询所有
    public List<Dept>selectAll();
    //添加分类
    public void insertDept(Dept dept);
    //多条件查询+分页
    public List<Dept> findDepts(@Param("dept") Dept dept, @Param("offset") Integer offset, @Param("limit") Integer limit);
    public List<Dept> findDeptList();
    public List<Dept> findDeptList1();
    public List<Dept> findDept(List list);
    //更新
    public void updateDept(@Param("dept") Dept dept);
}
package com.jie.mapper;

import com.jie.domain.Emp;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;

@Repository(value = "empMapper")
public interface EmpMapper {
    public List<HashMap<String,Object>> findEmpList();
    public List<Emp> findEmpList1();
    public List<Emp> findEmp(@Param("emp") Emp emp);
 }

service

接口忽略,同mapper接口一样(不带注解)

实现类

package com.jie.service.impl;

import com.jie.domain.Dept;
import com.jie.mapper.DeptMapper;
import com.jie.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service(value = "deptService")
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;

    @Override
    public Dept selectBreed(int id) {
        return null;
    }

    @Override
    public List<Dept> selectAll() {
        List list=deptMapper.selectAll();
        return list;
    }

    @Override
    public void insertDept(Dept dept) {
        deptMapper.insertDept(dept);
    }

    @Override
    public List<Dept> findDepts(Dept dept, Integer offset, Integer limit) {
        List list=deptMapper.findDepts(dept,offset,limit);
        return list;
    }

    @Override
    public List<Dept> findDeptList() {
        List list = deptMapper.findDeptList();
        return list;
    }

    @Override
    public List<Dept> findDeptList1() {
        List list=deptMapper.findDeptList1();
        return list;
    }

    @Override
    public List<Dept> findDept(List list) {
        List list1=deptMapper.findDept(list);
        return list1;
    }

    @Override
    public void updateDept(Dept dept) {
        deptMapper.updateDept(dept);
    }
}
package com.jie.service.impl;

import com.jie.domain.Emp;
import com.jie.mapper.EmpMapper;
import com.jie.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service(value = "empService")
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;


    @Override
    public List<HashMap<String, Object>> findEmpList() {
        List<HashMap<String,Object>>list=empMapper.findEmpList();
        return list;
    }

    @Override
    public List<Emp> findEmpList1() {
        List list=empMapper.findEmpList1();
        return list;
    }

    @Override
    public List<Emp> findEmp(Emp emp) {
        List list=empMapper.findEmp(emp);
        return list;
    }
}

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 命名空间 唯一的-->
<mapper namespace="com.jie.mapper.DeptMapper">
    <!--查询 id:当前命名空间唯
        resultType 指定返回结果集对应实体类类型一-->
    <select id="selectDept" resultType="Dept">
       select * from dept where id = #{id}
   </select>
    <!--返回list集合-->
    <select id="selectAll" resultType="Dept">
        select * from dept
    </select>

    <insert id="insertDept" parameterType="Dept">
        insert into dept(deptno,dname,loc)
        values (seq_u_id.nextval,#{dname},#{loc})
    </insert>

    <select id="findDepts" resultType="Dept">
        <!--定义一个绑定变量 name指定变量名称-->
        <bind name="dname" value="'%'+dept.dname+'%'"></bind>
        <bind name="loc" value="'%'+dept.loc+'%'"></bind>
        select d.deptno,d.dname,d.loc from
        (select rownum rn,dept.* from dept
        <where>
            <if test="dept!=null and dept.dname!=null">
                and dname like #{dname}

            </if>
            <if test="dept!=null and dept.loc!=null">
                and loc like #{loc}
            </if>
        </where>
        )d where d.rn between #{offset} and #{limit}
    </select>


    <select id="findDeptList" resultMap="deptMap">
       select dept.*,emp.*
       from dept join emp
       on dept.deptno = emp.deptno
    </select>

    <resultMap id="deptMap" type="Dept" autoMapping="true">
        <!--一对多关联-->
        <collection property="empList" ofType="Emp" column="deptno"  autoMapping="true">

        </collection>
    </resultMap>

    <!--一对多关联第二种形式-->
    <select id="findDeptList1" resultMap="deptMap2">
       select * from dept
    </select>

    <resultMap id="deptMap2" type="Dept" autoMapping="true">
        <!--一对多关联-->
        <collection property="empList" ofType="Emp" column="deptno"  autoMapping="true" select="getEmpList">

        </collection>
    </resultMap>
    <select id="getEmpList" parameterType="java.lang.Integer" resultType="Emp">
        select * from emp where deptno=#{deptno}
    </select>

    <select id="findDept" resultType="Dept">
        select * from dept where deptno in
        <foreach item="item" index="index" collection="list"
                 open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <update id="updateDept">
        update dept
        <set>
            <if test="dept!=null and dept.dname != null">dept.dname=#{dept.dname},</if>
            <if test="dept!=null and dept.loc != null">dept.loc=#{dept.loc},</if>
        </set>
        where deptno=#{dept.deptno}
    </update>


</mapper>
<?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.jie.mapper.EmpMapper">
    <select id="findEmpList" resultType="HashMap">
        select emp.*,dept.dname,dept.loc
        from emp join dept
        on emp.deptno=dept.deptno
    </select>

    <select id="findEmpList1" resultMap="empMap" >
        select emp.*,dept.*
        from emp ,dept
        where emp.deptno = dept.deptno
    </select>
    <!--autoMapping  让列和属性自动对应-->
    <resultMap id="empMap" type="Emp" autoMapping="true">
        <id property="empno" column="empno"></id>
        <!--一对多关联-->
        <association property="dept" column="deptno" javaType="Dept" autoMapping="true">
            <id property="deptno" column="deptno"></id>
        </association>
    </resultMap>

    <select id="findEmp" resultType="Emp">
        <bind name="ename" value="'%'+emp.ename+'%'"></bind>
        <bind name="job" value="'%'+emp.job+'%'"></bind>
        select * from emp where 1=1
        <choose>
            <when test="emp!=null and emp.ename != null">
                AND emp.ename like #{ename}
            </when>
            <when test="emp!=null and emp.job != null">
                AND emp.job like #{job}
            </when>
            <otherwise>
                AND featured = 1
            </otherwise>
        </choose>
    </select>

</mapper>

controller层

package com.jie.controller;

import com.alibaba.fastjson.JSON;
import com.jie.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/dept")
public class DeptController {
    @Autowired
    private DeptService deptService;

    @RequestMapping("/selectAll")
    @ResponseBody
    public String selectAll(){
        List list=deptService.selectAll();
        return JSON.toJSONString(list);
    }

    @RequestMapping("/findDeptList")
    @ResponseBody
    public String findDeptList(){
        List list=deptService.findDeptList();
        return JSON.toJSONString(list);
    }

    @RequestMapping("/findDeptList1")
    @ResponseBody
    public String findDeptList1(){
        List list=deptService.findDeptList1();
        return JSON.toJSONString(list);
    }

    @RequestMapping("/findDept")
    @ResponseBody
    public String findDept(){
        List list1=new ArrayList();
        list1.add(5);
        list1.add(10);
        List list=deptService.findDept(list1);
        return JSON.toJSONString(list);
    }

}
package com.jie.controller;

import com.alibaba.fastjson.JSON;
import com.jie.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;

@Controller
@RequestMapping("/emp")
public class EmpController {
    @Autowired
    private EmpService empService;

    @RequestMapping("/findEmpList")
    @ResponseBody
    public String findEmpList(){

        List list=empService.findEmpList();
        return JSON.toJSONString(list);
    }

    @RequestMapping("/findEmpList1")
    @ResponseBody
    public String findEmpList1(){
        List list=empService.findEmpList1();
        return JSON.toJSONString(list);
    }

}

排序有点问题,将就着看吧!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值