目录
1.添加相关依赖 注:spring 5.0.1.RELEASE有冲突
1.1 添加spring相关依赖(5.0.2.RELEASE)
以下有一些是目前不需要的(但是大部分都是)自己仔细看然后提取需要的部分
2.创建spring配置文件applicationContext
4.1 在工程的pom文件中增加spring-test的依赖
4.2 创建BaseTestCase,并在该类上加上两个注解
4.3 使用idean的junit插件(JUnitGenerator V2.0)生成测试用例
2.2、通过mybatis-generator插件生成dao、mapper、model
注:使用mybatis-generator插件,pom文件添加支持
- "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.0modelVersion>
-
- <groupId>com.zkinggroupId>
- <artifactId>mybatis01artifactId>
- <version>1.0-SNAPSHOTversion>
- <packaging>warpackaging>
-
- <name>mybatis01 Maven Webappname>
-
- <url>http://www.example.comurl>
-
- <properties>
- <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
- <maven.compiler.source>1.8maven.compiler.source>
- <maven.compiler.target>1.8maven.compiler.target>
-
-
- <junit.version>4.12junit.version>
- <servlet.version>4.0.1servlet.version>
-
- <mybatis.version>3.4.5mybatis.version>
-
- <mysql.version>5.1.44mysql.version>
-
- <log4j2.version>2.9.1log4j2.version>
- properties>
-
- <dependencies>
-
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>${junit.version}version>
- <scope>testscope>
- dependency>
-
-
- <dependency>
- <groupId>javax.servletgroupId>
- <artifactId>javax.servlet-apiartifactId>
- <version>${servlet.version}version>
- dependency>
-
-
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatisartifactId>
- <version>${mybatis.version}version>
- dependency>
-
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>${mysql.version}version>
- dependency>
-
-
-
-
- <dependency>
- <groupId>org.apache.logging.log4jgroupId>
- <artifactId>log4j-coreartifactId>
- <version>${log4j2.version}version>
- dependency>
- <dependency>
- <groupId>org.apache.logging.log4jgroupId>
- <artifactId>log4j-apiartifactId>
- <version>${log4j2.version}version>
- dependency>
-
- <dependency>
- <groupId>org.apache.logging.log4jgroupId>
- <artifactId>log4j-webartifactId>
- <version>${log4j2.version}version>
- dependency>
-
-
-
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- <version>1.18.12version>
- <scope>providedscope>
- dependency>
-
-
- <dependency>
- <groupId>com.github.pagehelpergroupId>
- <artifactId>pagehelperartifactId>
- <version>5.1.2version>
- dependency>
-
- dependencies>
-
- <build>
- <finalName>mybatis01finalName>
- <plugins>
-
- <plugin>
- <groupId>org.apache.maven.pluginsgroupId>
- <artifactId>maven-compiler-pluginartifactId>
- <version>3.7.0version>
- <configuration>
- <source>${maven.compiler.source}source>
- <target>${maven.compiler.target}target>
- <encoding>${project.build.sourceEncoding}encoding>
- configuration>
- plugin>
-
- <plugin>
- <groupId>org.mybatis.generatorgroupId>
- <artifactId>mybatis-generator-maven-pluginartifactId>
- <version>1.3.2version>
- <dependencies>
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>${mysql.version}version>
- dependency>
- dependencies>
- <configuration>
- <overwrite>trueoverwrite>
- configuration>
- plugin>
-
- plugins>
- build>
- project>
-
-
- <context:annotation-config/>
-
- <context:component-scan base-package="com.zking.oa"/>
<context:property-placeholder location="classpath:jdbc.properties"/>
- <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
- destroy-method="close">
- <property name="driverClassName" value="${jdbc.driver}"/>
- <property name="url" value="${jdbc.url}"/>
- <property name="username" value="${jdbc.username}"/>
- <property name="password" value="${jdbc.password}"/>
-
- <property name="initialSize" value="10"/>
-
- <property name="maxTotal" value="100"/>
-
- <property name="maxIdle" value="50"/>
-
- <property name="minIdle" value="10"/>
-
-
- <property name="maxWaitMillis" value="-1"/>
- bean>
-
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
-
- <property name="dataSource" ref="dataSource"/>
-
- <property name="mapperLocations" value="classpath*:com/zking/ssm/**/mapper/*.xml"/>
-
- <property name="typeAliasesPackage" value="com/zking/ssm/**/model"/>
-
- <property name="plugins">
- <array>
- <bean class="com.github.pagehelper.PageInterceptor">
- <property name="properties">
- <value>
- helperDialect=mysql
- value>
- property>
- bean>
- array>
- property>
- bean>
-
-
-
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
-
-
- <property name="basePackage" value="com/zking/ssm/**/mapper"/>
- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
- bean>
- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource" />
- bean>
- <tx:annotation-driven transaction-manager="transactionManager" />
@Repository:将Mapper类声明为Bean
@Service:通常作用在业务层
@Controller:通常作用在控制层,将在Spring MVC中使用
@Component:是一个泛化的概念,仅仅表示spring中的一个组件(Bean),可以作用在任何层次@Scope:模式声明(singleton|prototype)
@Autowired:将自动在Spring上下文与其匹配(默认是类型匹配)的Bean,并自动注入到相应的地方
@Resource:后面没有任何内容,默认通过name属性去匹配bean,找不到再按type去匹配,任何一个不匹配都将报错问题:@Autowired和@Resource两个注解的区别:
1)@Autowired默认按照byType方式进行bean匹配,@Resource默认按照byName方式进行bean匹配
2)@Autowired是Spring的注解,@Resource是J2EE的注解,这个看一下导入注解的时候这两个注解的包名就一清二楚了
Spring属于第三方的,J2EE是Java自己的东西,因此,建议使用@Resource注解,以减少代码和Spring之间的耦合。@Transactional
注:个人感觉注解式事务比以前的声明式事务更加麻烦,要写的东西更多
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-testartifactId>
- <version>${spring.version}version>
- dependency>
使用AOP编程解决分页代码重复的问题
注1:AOP注解式开发
注2:AOP分页实现“PageBeanAspect.java”
注:dbcp是apache上的一个java 连接池项目
现在分成了3个大版本,不同的版本要求的JDK不同:
DBCP 2 compiles and runs under Java 7 only (JDBC 4.1)
DBCP 1.4 compiles and runs under Java 6 only (JDBC 4)
DBCP 1.3 compiles and runs under Java 1.4-5 only (JDBC 3)
- package com.zking.ssm.model;
-
- import java.util.ArrayList;
- import java.util.List;
-
- /**
- * 一个客户对应多个订单
- */
- public class Customer {
- private Integer customerId;
-
- private String customerName;
- //什么是关联关系,如果A与B关联,那么B将被A的属性
- //类A 客户(一方)
- //类B 订单(多方)
- //建立实体层面的关联关系映射
- private List
orders=new ArrayList<>(); -
-
- public Customer(Integer customerId, String customerName) {
- this.customerId = customerId;
- this.customerName = customerName;
- }
-
- public void setOrders(List
orders) { - this.orders = orders;
- }
-
- public List
getOrders() { - return orders;
- }
-
- public Customer() {
- super();
- }
-
- public Integer getCustomerId() {
- return customerId;
- }
-
- public void setCustomerId(Integer customerId) {
- this.customerId = customerId;
- }
-
- public String getCustomerName() {
- return customerName;
- }
-
- public void setCustomerName(String customerName) {
- this.customerName = customerName;
- }
-
- @Override
- public String toString() {
- return "Customer{" +
- "customerId=" + customerId +
- ", customerName='" + customerName + '\'' +
- ", orders=" + orders +
- '}';
- }
- }
- <select id="queryCustomerAll" resultMap="oneToMany">
- select * from t_customer c left join t_order o on c.customer_id=o.cid
- select>
(一对一,一对多,多对一,多对多)
一对多:一本书对应多种书本类型,例如:西游记 -> 神话、古典、名著
多对一:多本书指向一种书本类型,例如:西游记、山海经、聊斋志异 -> 神话
多对多:永远视为两个一对多
一个人负责多个项目开发,例如:张三负责 A B C
一个项目由多个人来开发,例如:A项目由张三、李四、王五来开发
1)配置mybatis-generator插件生成文件位置
2)修改generatorConfig.xml配置文件的生成目录(mapper和model)及对应生成关系
- "1.0" encoding="UTF-8" ?>
- generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
- "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
- <generatorConfiguration>
-
- <properties resource="jdbc.properties"/>
-
-
- <classPathEntry location="E:\computer_load\apache-maven-3.5.4\mvn_repository\mysql\mysql-connector-java\5.1.44\mysql-connector-java-5.1.44.jar"/>
-
-
- <context id="infoGuardian">
-
- <commentGenerator>
- <property name="suppressAllComments" value="true"/>
- <property name="suppressDate" value="true"/>
- commentGenerator>
-
-
- <jdbcConnection driverClass="${jdbc.driver}"
- connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}"/>
-
-
- <javaTypeResolver>
-
- <property name="forceBigDecimals" value="false"/>
- javaTypeResolver>
-
-
-
-
- <javaModelGenerator targetPackage="com.zking.ssm.model"
- targetProject="src/main/java">
-
- <property name="enableSubPackages" value="false"/>
-
- <property name="constructorBased" value="true"/>
-
- <property name="trimStrings" value="false"/>
-
- <property name="immutable" value="false"/>
- javaModelGenerator>
-
-
- <sqlMapGenerator targetPackage="com.zking.ssm.mapper"
- targetProject="src/main/resources">
-
- <property name="enableSubPackages" value="false"/>
- sqlMapGenerator>
-
-
-
-
-
- <javaClientGenerator targetPackage="com.zking.ssm.mapper"
- targetProject="src/main/java" type="XMLMAPPER">
-
- <property name="enableSubPackages" value="false"/>
- javaClientGenerator>
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- context>
- generatorConfiguration>
- <resultMap id="one2many" type="Customer">
- <id column="customer_id" property="customerId"/>
- <result column="customer_name" property="customerName"/>
-
-
- <collection property="orders" ofType="Order">
- <id column="order_id" property="orderId"/>
- <result column="order_no" property="orderNo"/>
- collection>
- resultMap>
- <resultMap id="many2one" type="Order">
- <id column="order_id" property="orderId"/>
- <result column="order_no" property="orderNo"/>
- <result column="cid" property="cid"/>
-
-
- <association property="customer" javaType="Customer">
- <id column="customer_id" property="customerId"/>
- <result column="customer_name" property="customerName"/>
- association>
- resultMap>