引言
既然要学习mybats,环境是必须的了。单独学习mybatis,对于Java开发者来看已经没有太大的意义,因此本次搭建的环境是 spring + mybatis。
开发工具
- idea 2018.2.4 x64
- MySQL
- jdk1.8
- win10
框架
- mybatis3.4.1
- spring 4.3.5.RELEASE
搭建过程
建工程
- 建立webapp工程
- 项目引入之后等待maven构建完成,maven的结构通常不是标准开发目录结构,因此我们要新建目录
- 在main目录下新建java和resources文件夹。
- 需要给这些目录加上对应的标识,简单的方式是:右击项目
- 选中maven确认即可
引入包(可以逐个加入看效果)
- 从给出的pom文件(见附录pom)中复制需要的jar信息内容到你的pom文件。
- 新建spring需要的.xml文件
- applicationContext.xml
- 配置自动包扫描: <context:component-scan base-package=“com.sswh”/>
- mvc注解驱动: mvc:annotation-driven/
- mvc静态资源访问:mvc:default-servlet-handler/
- 创建数据源:com.mchange.v2.c3p0.ComboPooledDataSource
- mybatis管理类:org.mybatis.spring.SqlSessionFactoryBean
- mybatis映射类:org.mybatis.spring.mapper.MapperScannerConfigurer
- 事务管理:org.springframework.jdbc.datasource.DataSourceTransactionManager
- 配置基于事务管理的注解驱动:
<tx:annotation-driven transaction-manager=“transactionManager”/> - 配置前缀后缀:org.springframework.web.servlet.view.InternalResourceViewResolver
- applicationContext.xml
- 新建mybatsi需要的.xml文件
- sqlMapperConfig.xml 该类在集成spring之后需要配置的很少
- *mapperDao.xml :该配置文件用于写业务的sql。用于实现业务的接口。
SqlSessionFactoryBean 的属性包含扫描位置的路径变量mapperLocations:
启动
- 建如下的测试文件
- Application.java
package com.sswh;
import org.apache.ibatis.io.ResolverUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by xxx
*/
public class Application {
public static void main(String[] args) {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
BaseEntity baseEntity = (BaseEntity)ac.getBean("baseEntity");
baseEntity.setName("name");
String name = baseEntity.getName();
String test = test(baseEntity.getName());
int b = 0;
for(int i=0;i<100;i++){
System.out.println("I:"+i);
b = i;
int c = 10;
}
System.out.println(test);
}
public static String test(String name){
return "fix:"+name;
}
}
- BaseDao.java
package com.sswh;
/**
* Created by xxx
*/
public class BaseDao {
}
- BaseEntity.java
package com.sswh;
/**
* Created by xxx
*/
public class BaseEntity {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
- applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/aop http://www.springframework.org/schema/aop/spring-aop.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:component-scan base-package="com.sswh"/>
<!-- mvc的注解驱动 -->
<mvc:annotation-driven/>
<!-- mvc静态资源的访问 -->
<mvc:default-servlet-handler/>
<!-- 加载db.properties配置文件 -->
<!-- <context:property-placeholder location="classpath:db.properties"/> -->
<!-- 创建数据源 c3p0 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/easybuy"/>
<property name="user" value="root"/>
<property name="password" value="aaron"/>
</bean>
<!-- 创建mybaits的sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="mapperLocations" value="classpath*:com/sswh/**/*Dao.xml"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="baseEntity" class="com.sswh.BaseEntity"></bean>
<bean id="baseDao" class="com.sswh.BaseDao"></bean>
</beans>
- baseDao.xml mybatis接口实现
<?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.sswh.BaseDao">
</mapper>
检查
- 检查xml文件的配置路径是否正确
- 检查包的导入情况
结语
以上就可以顺利的跑起一个spring mybatis的基本模块了。2019年2月1日 13:22:53
附录
pom
<?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.sswh</groupId>
<artifactId>mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>mybatis Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- spring版本号 -->
<spring.version>4.3.5.RELEASE</spring.version>
<!-- mybatis版本号 -->
<mybatis.version>3.4.1</mybatis.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 数据库 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
<scope>runtime</scope>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- mybatis/spring整合包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
<build>
<finalName>mybatis</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>