三大框架整合. mybatis逆向工程 1.三大框架整合 在老期的项目中,一般都是使用ssm项目做开发的,虽然现在的主流开发是springboot来做开发,但是ssm的基本整合还是需要掌握的。
1.导入pom文件 1、导入spring的pom依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 <?xml version="1.0" encoding="UTF-8" ?> <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/xsd/maven-4.0.0.xsd" > <modelVersion > 4.0.0</modelVersion > <groupId > com.mashibing</groupId > <artifactId > ssm</artifactId > <version > 1.0-SNAPSHOT</version > <dependencies > <dependency > <groupId > junit</groupId > <artifactId > junit</artifactId > <version > 4.12</version > <scope > test</scope > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-context</artifactId > <version > 5.2.3.RELEASE</version > </dependency > <dependency > <groupId > cglib</groupId > <artifactId > cglib</artifactId > <version > 3.3.0</version > </dependency > <dependency > <groupId > org.aspectj</groupId > <artifactId > aspectjweaver</artifactId > <version > 1.9.5</version > </dependency > <dependency > <groupId > aopalliance</groupId > <artifactId > aopalliance</artifactId > <version > 1.0</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-aspects</artifactId > <version > 5.2.3.RELEASE</version > </dependency > <dependency > <groupId > com.alibaba</groupId > <artifactId > druid</artifactId > <version > 1.1.21</version > </dependency > <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > <version > 5.1.48</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-web</artifactId > <version > 5.2.3.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-webmvc</artifactId > <version > 5.2.3.RELEASE</version > </dependency > <dependency > <groupId > javax.servlet</groupId > <artifactId > servlet-api</artifactId > <version > 2.5</version > <scope > provided</scope > </dependency > <dependency > <groupId > javax.servlet</groupId > <artifactId > jsp-api</artifactId > <version > 2.0</version > <scope > provided</scope > </dependency > <dependency > <groupId > com.fasterxml.jackson.core</groupId > <artifactId > jackson-core</artifactId > <version > 2.10.3</version > </dependency > <dependency > <groupId > com.fasterxml.jackson.core</groupId > <artifactId > jackson-databind</artifactId > <version > 2.10.3</version > </dependency > <dependency > <groupId > com.fasterxml.jackson.core</groupId > <artifactId > jackson-annotations</artifactId > <version > 2.10.3</version > </dependency > <dependency > <groupId > commons-io</groupId > <artifactId > commons-io</artifactId > <version > 2.6</version > </dependency > <dependency > <groupId > commons-fileupload</groupId > <artifactId > commons-fileupload</artifactId > <version > 1.4</version > </dependency > <dependency > <groupId > javax.servlet</groupId > <artifactId > jstl</artifactId > <version > 1.2</version > </dependency > <dependency > <groupId > org.mybatis</groupId > <artifactId > mybatis</artifactId > <version > 3.5.4</version > </dependency > <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > <version > 8.0.16</version > </dependency > <dependency > <groupId > log4j</groupId > <artifactId > log4j</artifactId > <version > 1.2.17</version > </dependency > <dependency > <groupId > org.mybatis</groupId > <artifactId > mybatis-spring</artifactId > <version > 2.0.4</version > </dependency > </dependencies > </project >
2.编写各个框架的配置文件 web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 <?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" > <listener > <listener-class > org.springframework.web.context.ContextLoaderListener</listener-class > </listener > <context-param > <param-name > contextConfigLocation</param-name > <param-value > classpath:spring.xml</param-value > </context-param > <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:springmvc.xml</param-value > </init-param > <load-on-startup > 1</load-on-startup > </servlet > <servlet-mapping > <servlet-name > springmvc</servlet-name > <url-pattern > /</url-pattern > </servlet-mapping > <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 > <filter > <filter-name > HiddenHttpMethodFilter</filter-name > <filter-class > org.springframework.web.filter.HiddenHttpMethodFilter</filter-class > </filter > <filter-mapping > <filter-name > HiddenHttpMethodFilter</filter-name > <url-pattern > /*</url-pattern > </filter-mapping > </web-app >
springmvc.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 <?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" > <context:component-scan base-package ="com.mashibing" use-default-filters ="false" > <context:include-filter type ="annotation" expression ="org.springframework.stereotype.Controller" /> </context:component-scan > <mvc:default-servlet-handler > </mvc:default-servlet-handler > <mvc:annotation-driven > </mvc:annotation-driven > <bean class ="org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name ="prefix" value ="/WEB-INF/page/" > </property > <property name ="suffix" value =".jsp" > </property > </bean > </beans >
spring.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 <?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" 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 https://www.springframework.org/schema/tx/spring-tx.xsd " > <context:component-scan base-package ="com.mashibing" > <context:exclude-filter type ="annotation" expression ="org.springframework.stereotype.Controller" /> </context:component-scan > <context:property-placeholder location ="classpath:db.properties" > </context:property-placeholder > <bean id ="dataSource" class ="com.alibaba.druid.pool.DruidDataSource" > <property name ="username" value ="${jdbc.username}" > </property > <property name ="password" value ="${jdbc.password}" > </property > <property name ="driverClassName" value ="${jdbc.driverName}" > </property > <property name ="url" value ="${jdbc.url}" > </property > </bean > <bean id ="transactionManager" class ="org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property name ="dataSource" ref ="dataSource" > </property > </bean > <tx:annotation-driven transaction-manager ="transactionManager" > </tx:annotation-driven > <bean id ="sqlSessionFactory" class ="org.mybatis.spring.SqlSessionFactoryBean" > <property name ="configLocation" value ="classpath:mybatis-config.xml" > </property > <property name ="dataSource" ref ="dataSource" > </property > <property name ="mapperLocations" value ="classpath:com/mashibing/dao/*.xml" > </property > </bean > <bean class ="org.mybatis.spring.mapper.MapperScannerConfigurer" > <property name ="basePackage" value ="com.mashibing.dao" > </property > <property name ="sqlSessionFactoryBeanName" value ="sqlSessionFactory" > </property > </bean > </beans >
mybatis-config.xml
1 2 3 4 5 6 7 8 9 10 11 12 <?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 ="mapUnderscoreToCamelCase" value ="true" /> </settings > <typeAliases > <package name ="com.mashibing.bean" /> </typeAliases > </configuration >
db.properties
1 2 3 4 jdbc.username =root jdbc.password =123456 jdbc.url =jdbc:mysql://192.168.85.111:3306/demo?serverTimezone=UTC jdbc.driverName =com.mysql.jdbc.Driver
拷贝映射文件,还是位于com.mashibing.dao目录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 <?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.mashibing.dao.EmpDao" > <select id ="findEmpByEmpno" resultType ="emp" > select * from emp where empno = #{empno} </select > <insert id ="insertEmp" > insert into emp(empno,ename) values(#{empno},#{ename}) </insert > <update id ="updateEmp" > update emp set ename=#{ename} where empno = #{empno} </update > <delete id ="deleteEmp" > delete from emp where empno = #{empno} </delete > <select id ="selectEmpByNoAndName" resultType ="com.mashibing.bean.Emp" > select * from ${t} where empno=${empno} and ename=${ename} </select > <select id ="selectEmpByNoAndName2" resultType ="com.mashibing.bean.Emp" > select * from emp where empno=#{empno} and ename=#{ename} </select > <select id ="selectAllEmp" resultType ="com.mashibing.bean.Emp" > select * from emp </select > <select id ="selectEmpByEmpReturnMap" resultType ="map" > select * from emp where empno = #{empno} </select > <select id ="getAllEmpReturnMap" resultType ="com.mashibing.bean.Emp" > select * from emp </select > <select id ="selectEmpAndDept" resultMap ="empDept" > select * from emp left join dept on emp.deptno = dept.deptno where empno = #{empno}; </select > <resultMap id ="empDept" type ="com.mashibing.bean.Emp" > <id column ="empno" property ="empno" > </id > <result column ="ename" property ="ename" > </result > <result column ="job" property ="job" > </result > <result column ="mgr" property ="mgr" > </result > <result column ="hiredate" property ="hiredate" > </result > <result column ="sal" property ="sal" > </result > <result column ="comm" property ="common" > </result > <association property ="dept" javaType ="com.mashibing.bean.Dept" > <id column ="deptno" property ="deptno" > </id > <result column ="dname" property ="dname" > </result > <result column ="loc" property ="loc" > </result > </association > </resultMap > <select id ="selectEmpAndDeptBySimple" resultMap ="simpleEmpAndDept" > select * from emp where empno = #{empno} </select > <resultMap id ="simpleEmpAndDept" type ="com.mashibing.bean.Emp" > <id column ="empno" property ="empno" > </id > <result column ="ename" property ="ename" > </result > <result column ="job" property ="job" > </result > <result column ="mgr" property ="mgr" > </result > <result column ="hiredate" property ="hiredate" > </result > <result column ="sal" property ="sal" > </result > <result column ="comm" property ="common" > </result > <association property ="dept" select ="com.mashibing.dao.DeptDao.getDeptAndEmpsBySimple" column ="deptno" fetchType ="eager" > </association > </resultMap > <select id ="selectEmpByStep" resultType ="com.mashibing.bean.Emp" > select * from emp where deptno = #{deptno} </select > <select id ="getEmpByCondition" resultType ="com.mashibing.bean.Emp" > select * from emp <trim prefix ="where" prefixOverrides ="and" suffixOverrides ="and" > <if test ="empno!=null" > empno > #{empno} and </if > <if test ="ename!=null" > ename like #{ename} and </if > <if test ="sal!=null" > sal > #{sal} and </if > </trim > </select > <select id ="getEmpByDeptnos" resultType ="Emp" > select * from emp where deptno in <foreach collection ="deptnos" close =")" index ="idx" item ="deptno" open ="(" separator ="," > #{deptno} </foreach > </select > <select id ="getEmpByConditionChoose" resultType ="com.mashibing.bean.Emp" > select * from emp <where > <choose > <when test ="empno!=null" > empno > #{empno} </when > <when test ="ename!=null" > ename like #{ename} </when > <when test ="sal!=null" > sal > #{sal} </when > <otherwise > 1=1 </otherwise > </choose > </where > </select > <update id ="updateEmpByEmpno" > update emp <set > <if test ="empno!=null" > empno=#{empno}, </if > <if test ="ename!=null" > ename = #{ename}, </if > <if test ="sal!=null" > @ sal = #{sal} </if > </set > <where > empno = #{empno} </where > </update > </mapper >
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 <?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.mashibing.dao.DeptDao" > <select id ="getDeptAndEmps" resultMap ="deptEmp" > select * from dept left join emp on dept.deptno = emp.deptno where dept.deptno=#{deptno} </select > <resultMap id ="deptEmp" type ="com.mashibing.bean.Dept" > <id property ="deptno" column ="deptno" > </id > <result property ="dname" column ="dname" > </result > <result property ="loc" column ="loc" > </result > <collection property ="emps" ofType ="com.mashibing.bean.Emp" > <id property ="empno" column ="empno" > </id > <result column ="ename" property ="ename" > </result > <result column ="job" property ="job" > </result > <result column ="mgr" property ="mgr" > </result > <result column ="hiredate" property ="hiredate" > </result > <result column ="sal" property ="sal" > </result > <result column ="comm" property ="common" > </result > </collection > </resultMap > <select id ="getDeptAndEmpsBySimple" resultType ="com.mashibing.bean.Dept" > select * from dept where deptno = #{deptno} </select > <select id ="getDeptAndEmpsByStep" resultMap ="deptEmpByStep" > select * from dept where deptno = #{deptno} </select > <resultMap id ="deptEmpByStep" type ="com.mashibing.bean.Dept" > <id property ="deptno" column ="deptno" > </id > <result property ="dname" column ="dname" > </result > <result property ="loc" column ="loc" > </result > <collection property ="emps" ofType ="com.mashibing.bean.Emp" select ="com.mashibing.dao.EmpDao.selectEmpByStep" column ="deptno" > </collection > </resultMap > </mapper >
添加dao的接口
EmpDao.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 package com.mashibing.dao;import com.mashibing.bean.Emp;import org.apache.ibatis.annotations.MapKey;import org.apache.ibatis.annotations.Param;import java.util.List;import java.util.Map;public interface EmpDao { public Emp findEmpByEmpno (Integer empno) ; public int updateEmp (Emp emp) ; public int deleteEmp (Integer empno) ; public int insertEmp (Emp emp) ; Emp selectEmpByNoAndName (@Param("empno") Integer empno, @Param("ename") String ename, @Param("t") String tablename) ; Emp selectEmpByNoAndName2 (Map<String, Object> map) ; List<Emp> selectAllEmp () ; Map<String,Object> selectEmpByEmpReturnMap (Integer empno) ; @MapKey("empno") Map<Integer,Emp> getAllEmpReturnMap () ; Emp selectEmpAndDept (Integer empno) ; Emp selectEmpAndDeptBySimple (Integer empno) ; List<Emp> selectEmpByStep (Integer deptno) ; public List<Emp> getEmpByCondition (Emp emp) ; public List<Emp> getEmpByConditionChoose (Emp emp) ; public List<Emp> getEmpByDeptnos (@Param("deptnos") List<Integer> deptnos) ; public int updateEmpByEmpno (Emp emp) ; }
3.创建测试类 TestController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 package com.mashibing.controller;import com.mashibing.bean.Emp;import com.mashibing.dao.EmpDao;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controller public class TestController { @Autowired EmpDao empDao; @RequestMapping("/test") public String test () { System.out.println("test" ); Emp empByEmpno = empDao.findEmpByEmpno(7369 ); System.out.println(empByEmpno); return "success" ; } }
2.mybatis逆向工程 引入pom依赖
1 2 3 4 5 6 <dependency > <groupId > org.mybatis.generator</groupId > <artifactId > mybatis-generator-core</artifactId > <version > 1.4.0</version > </dependency >
编写配置文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" > <generatorConfiguration > <context id ="simple" targetRuntime ="MyBatis3" > <jdbcConnection driverClass ="com.mysql.cj.jdbc.Driver" connectionURL ="jdbc:mysql://192.168.85.111:3306/demo?serverTimezone=UTC" userId ="root" password ="123456" /> <javaModelGenerator targetPackage ="com.mashibing.bean" targetProject ="src/main/java" /> <sqlMapGenerator targetPackage ="com.mashibing.dao" targetProject ="src/main/resources" /> <javaClientGenerator type ="XMLMAPPER" targetPackage ="com.mashibing.dao" targetProject ="src/main/java" /> <table tableName ="emp" domainObjectName ="Emp" enableCountByExample ="false" enableDeleteByExample ="false" enableUpdateByExample ="false" selectByExampleQueryId ="false" enableSelectByExample ="false" /> <table tableName ="dept" domainObjectName ="Dept" enableCountByExample ="false" enableDeleteByExample ="false" enableUpdateByExample ="false" selectByExampleQueryId ="false" enableSelectByExample ="false" /> </context > </generatorConfiguration >
编写测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 package com.mashibing;import org.mybatis.generator.api.MyBatisGenerator;import org.mybatis.generator.config.Configuration;import org.mybatis.generator.config.xml.ConfigurationParser;import org.mybatis.generator.exception.InvalidConfigurationException;import org.mybatis.generator.exception.XMLParserException;import org.mybatis.generator.internal.DefaultShellCallback;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;public class Test { public static void main (String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException { List<String> warnings = new ArrayList <String>(); boolean overwrite = true ; File configFile = new File ("mbg.xml" ); ConfigurationParser cp = new ConfigurationParser (warnings); Configuration config = cp.parseConfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback (overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator (config, callback, warnings); myBatisGenerator.generate(null ); } }
3.分页插件 PageHelper(自学,项目中会使用)