1.新建maven项目
2.在pom.xml中添加jar包
懒人(本人乃其中一个)请复制:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>top.bwextend</groupId> 8 <artifactId>blog</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <properties> 12 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 13 <spring.version>4.3.0.RELEASE</spring.version> 14 </properties> 15 16 <dependencies> 17 <!--Spring框架核心库 --> 18 <dependency> 19 <groupId>org.springframework</groupId> 20 <artifactId>spring-context</artifactId> 21 <version>${spring.version}</version> 22 </dependency> 23 <!-- Spring MVC --> 24 <dependency> 25 <groupId>org.springframework</groupId> 26 <artifactId>spring-webmvc</artifactId> 27 <version>${spring.version}</version> 28 </dependency> 29 <dependency> 30 <groupId>org.springframework</groupId> 31 <artifactId>spring-context-support</artifactId> 32 <version>${spring.version}</version> 33 </dependency> 34 <!-- aspectJ AOP 织入器 --> 35 <dependency> 36 <groupId>org.aspectj</groupId> 37 <artifactId>aspectjweaver</artifactId> 38 <version>1.8.9</version> 39 </dependency> 40 <!--mybatis-spring适配器 --> 41 <dependency> 42 <groupId>org.mybatis</groupId> 43 <artifactId>mybatis-spring</artifactId> 44 <version>1.3.0</version> 45 </dependency> 46 <!--Spring java数据库访问包,在本例中主要用于提供数据源 --> 47 <dependency> 48 <groupId>org.springframework</groupId> 49 <artifactId>spring-jdbc</artifactId> 50 <version>${spring.version}</version> 51 </dependency> 52 <!--log4j日志包 --> 53 <dependency> 54 <groupId>org.apache.logging.log4j</groupId> 55 <artifactId>log4j-core</artifactId> 56 <version>2.6.1</version> 57 </dependency> 58 <!-- mybatis ORM框架 --> 59 <dependency> 60 <groupId>org.mybatis</groupId> 61 <artifactId>mybatis</artifactId> 62 <version>3.4.1</version> 63 </dependency> 64 <!-- JUnit单元测试工具 --> 65 <dependency> 66 <groupId>junit</groupId> 67 <artifactId>junit</artifactId> 68 <version>4.10</version> 69 </dependency> 70 <!--c3p0 连接池 --> 71 <dependency> 72 <groupId>c3p0</groupId> 73 <artifactId>c3p0</artifactId> 74 <version>0.9.1.2</version> 75 </dependency> 76 <!-- JSTL --> 77 <dependency> 78 <groupId>javax.servlet</groupId> 79 <artifactId>jstl</artifactId> 80 <version>1.2</version> 81 </dependency> 82 <!-- Servlet核心包 --> 83 <dependency> 84 <groupId>javax.servlet</groupId> 85 <artifactId>javax.servlet-api</artifactId> 86 <version>3.0.1</version> 87 <scope>provided</scope> 88 </dependency> 89 <!--JSP --> 90 <dependency> 91 <groupId>javax.servlet.jsp</groupId> 92 <artifactId>jsp-api</artifactId> 93 <version>2.1</version> 94 <scope>provided</scope> 95 </dependency> 96 <!-- https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-client --> 97 <dependency> 98 <groupId>org.mariadb.jdbc</groupId> 99 <artifactId>mariadb-java-client</artifactId> 100 <version>2.2.3</version> 101 </dependency> 102 <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload --> 103 <dependency> 104 <groupId>commons-fileupload</groupId> 105 <artifactId>commons-fileupload</artifactId> 106 <version>1.3.3</version> 107 </dependency> 108 109 <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> 110 <dependency> 111 <groupId>com.fasterxml.jackson.core</groupId> 112 <artifactId>jackson-databind</artifactId> 113 <version>2.9.5</version> 114 </dependency> 115 116 </dependencies> 117 118 </project>
3.创建数据库,然后创建表,然后导出
4.在java项目根目录中新建一个文件夹用于存放导出的sql文件
5.新建一个文件存放db连接四要素,文件名为:db.properties
懒人(本人乃其中一个)请复制:
(注:my.url中最后的buyBook为数据库的名字,自行修改)
1 my.driver=org.mariadb.jdbc.Driver 2 my.url=jdbc:mariadb://localhost:3306/buyBook 3 my.username=bwextend 4 my.password=lbb961111
6.对项目进行分层,简单分层为:dao层,server层,model层(其实就是创建三个文件夹)
7.构建model层(就是一些java Bean)
8.构建dao层(mapper接口+mapper)
注:namespace参数为mapper接口全限定名,自行修改。
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <mapper namespace="top.bwextend.dao.book.IBookDao"> 6 <!--具体增删改查操作--> 7 </mapper>
9.编写spring配置文件
懒人(本人乃其中一个)请复制:
注:引用属性文件位置、数据源中四要素、工厂bean中别名扫描的范围及sql映射文件位置、自动扫描对象关系映射中接口所在包、Ioc自动扫描的范围自行更改。
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> 10 11 <!--1 引入属性文件,在配置中占位使用 --> 12 <context:property-placeholder location="db.properties" /> 13 14 <!--2 配置C3P0数据源 --> 15 <bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> 16 <!--驱动类名 --> 17 <property name="driverClass" value="${my.driver}" /> 18 <!-- url --> 19 <property name="jdbcUrl" value="${my.url}" /> 20 <!-- 用户名 --> 21 <property name="user" value="${my.username}" /> 22 <!-- 密码 --> 23 <property name="password" value="${my.password}" /> 24 <!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数 --> 25 <property name="acquireIncrement" value="5"></property> 26 <!-- 初始连接池大小 --> 27 <property name="initialPoolSize" value="10"></property> 28 <!-- 连接池中连接最小个数 --> 29 <property name="minPoolSize" value="5"></property> 30 <!-- 连接池中连接最大个数 --> 31 <property name="maxPoolSize" value="20"></property> 32 </bean> 33 34 <!--3 会话工厂bean sqlSessionFactoryBean --> 35 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 36 <!-- 数据源 --> 37 <property name="dataSource" ref="datasource"></property> 38 <!-- 别名 --> 39 <property name="typeAliasesPackage" value="top.bwextend.model"></property> 40 <!-- sql映射文件路径 --> 41 <property name="mapperLocations" value="classpath*:top/bwextend/dao/*Dao.xml"></property> 42 </bean> 43 44 <!--4 自动扫描对象关系映射 --> 45 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 46 <!--指定会话工厂,如果当前上下文中只定义了一个则该属性可省去 --> 47 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> 48 <!-- 指定要自动扫描接口的基础包,实现接口 --> 49 <property name="basePackage" value="top.bwextend.dao"></property> 50 </bean> 51 52 <!--5 声明式事务管理 --> 53 <!--定义事物管理器,由spring管理事务 --> 54 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 55 <property name="dataSource" ref="datasource"></property> 56 </bean> 57 <!--支持注解驱动的事务管理,指定事务管理器 --> 58 <tx:annotation-driven transaction-manager="transactionManager"/> 59 60 <!--6 容器自动扫描IOC组件 --> 61 <context:component-scan base-package="top.bwextend"></context:component-scan> 62 63 <!--7 aspectj支持自动代理实现AOP功能 --> 64 <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy> 65 66 </beans>
10.构建server层
注:
(1)@Service与@Component作用等效;
(2)@Resource是域属性注解,不携带任何参数意为按类型注入,携带name参数则为按名称注入;
(3)@Transactional是事务注解。
11.测试
注:tt_01()为正常测试,tt_02()为测试事务。