Mybatis+Spring的整合练习
一、建立数据库、建表
二、新建maven项目
三、添加依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.0</version>
</dependency> <dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency> <!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<!-- c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency> </dependencies>
四、建立包,搭建项目架构
五、根据数据库写对应的pojo类
package com.dongtian.MyBatis_Spring.pojo; import java.util.Date; public class User {
private int id;
private String username;
private Date birthday;
private String sex;
private String address; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public Date getBirthday() {
return birthday;
} public void setBirthday(Date birthday) {
this.birthday = birthday;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} @Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", birthday=" + birthday + ", sex=" + sex + ", address="
+ address + "]";
}
}
六、写映射器和接口
package com.dongtian.MyBatis_Spring.mapper; import org.springframework.stereotype.Repository; import com.dongtian.MyBatis_Spring.pojo.User;
@Repository
public interface UserMapper {
public void insertUser(User user);
}
<?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.dongtian.MyBatis_Spring.mapper.UserMapper">
<insert id="insertUser" parameterType="com.dongtian.MyBatis_Spring.pojo.User">
insert into user(username,sex,address)
values(#{username},#{sex},#{address})
</insert>
</mapper>
七、写业务层接口和接口实现类
package com.dongtian.MyBatis_Spring.service; import com.dongtian.MyBatis_Spring.pojo.User; public interface UserService {
public void insertUser(User user);
}
package com.dongtian.MyBatis_Spring.serviceImp; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import com.dongtian.MyBatis_Spring.mapper.UserMapper;
import com.dongtian.MyBatis_Spring.pojo.User;
import com.dongtian.MyBatis_Spring.service.UserService; @Service
public class UserServiceImp implements UserService{
@Autowired
private UserMapper userMapper = null; @Transactional(propagation = Propagation.REQUIRES_NEW,
isolation=Isolation.READ_COMMITTED
)
public void insertUser(User user) {
userMapper.insertUser(user);
} }
八、简单测试
package com.dongtian.MyBatis_Spring; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.dongtian.MyBatis_Spring.pojo.User;
import com.dongtian.MyBatis_Spring.service.UserService; public class TestMyDemo {
public static void main(String[] args) {
//加载配置文件
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
//获取代理对象
UserService userService = applicationContext.getBean(UserService.class);
//通过接口对象调用接口方法
User user = new User();
user.setUsername("王力宏");
user.setSex("男");
user.setAddress("上海");
userService.insertUser(user);
System.out.println(user);
}
}
九、流程逻辑比较简单:
通过拿到业务层接口的代理对象,代理对象调用接口中的方法,接口方法调用映射器中的方法,由mapper操作数据库。
十、重点在配置文件中
(1)sqlMapConfig.xml : 大部分事情都交给了spring
<?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>
<mappers>
<package name="com.dongtian.MyBatis_Spring.mapper"/>
</mappers>
</configuration>
(2)applicationContext.xml
<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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 启动扫描机制 -->
<context:annotation-config/>
<context:component-scan base-package="com.dongtian.MyBatis_Spring.*"/>
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:db.properties" />
<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean> <!-- sqlSessinFactory -->
<bean id="sqlSFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="sqlMapConfig.xml" />
<property name="dataSource" ref="datasource" />
</bean> <!-- mapper批量扫描,从mapper包中扫描出mapper接口,自动创建代理对象并且在spring容器中注册
自动扫描出来的mapper的bean的id为mapper类名(首字母小写)
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.dongtian.MyBatis_Spring.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSFactory"/>
<property name="annotationClass" value="org.springframework.stereotype.Repository"></property>
</bean> <!-- 事务管理器配置数据源事务 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datasource"></property>
</bean>
<!-- 使用注解来定义事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/> </beans>
Mybatis+Spring的整合练习的更多相关文章
- 学习spring第五天 mybatis+spring的整合(maven多模块数据查询使用了分页和连接池),以及aop
mybatis+spring的整合: 导入的依赖:1.数据库连接:mysql-connector-java 2.连接池:druid 3.servlet:javax.servlet-api 4.jstl ...
- Mybatis+Spring框架整合
1.整合思路 1.SqlSessionFactory对象应该放到spring容器中作为单例存在. 2.传统dao的开发方式中,应该从spring容器中获得sqlsession对象. 3.Mapper代 ...
- mybatis spring 框架整合
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test user=LF password=LF <?xml versi ...
- springMVC+MyBatis+Spring+maven 整合(1)
1.首先第一步.部署mybatis ; 1.1 下载myBatis MyBits前身是iBitis,相对于Hibernate而言,它是半自动化ORM框架.由于老板对性能要求的比较苛刻,不得不放弃我亲爱 ...
- MyBatis+Spring+Spring MVC整合开发
MyBatis+Spring+Spring MVC整合开发课程观看地址:http://www.xuetuwuyou.com/course/65课程出自学途无忧网:http://www.xuetuwuy ...
- idea spring+springmvc+mybatis环境配置整合详解
idea spring+springmvc+mybatis环境配置整合详解 1.配置整合前所需准备的环境: 1.1:jdk1.8 1.2:idea2017.1.5 1.3:Maven 3.5.2 2. ...
- Spring+Mybatis基于注解整合Redis
基于这段时间折腾redis遇到了各种问题,想着整理一下.本文主要介绍基于Spring+Mybatis以注解的形式整合Redis.废话少说,进入正题. 首先准备Redis,我下的是Windows版,下载 ...
- 【Java EE 学习 79 下】【动态SQL】【mybatis和spring的整合】
一.动态SQL 什么是动态SQL,就是在不同的条件下,sql语句不相同的意思,曾经在“酒店会员管理系统”中写过大量的多条件查询,那是在SSH的环境中,所以只能在代码中进行判断,以下是其中一个多条件查询 ...
- springMVC+mybatis+spring整合案例
1.web.xml a:配置spring监听,使web容器在启动时加载spring的applicationContext.xml <listener> <listener-class ...
随机推荐
- 解决1130-host'192.168.2.137'is not allowed to connect to this mysql server报错问题
连接数据库服务器出现1130-host'192.168.2.137'is not allowed to connect to this mysql server错误, 这个问题是因为在数据库服务器中的 ...
- 使用session在jsp页面之间传递多维数组,用于实现全局变量的效果
使用session在jsp页面之间传递多维数组:发送数据的jsp页面:int [][] form_number=new int[4][4]; session.setAttribute("se ...
- Oracle 设置NLS_CHARACTERSET字符集
Oracle 设置NLS_CHARACTERSET字符集 sqlplus "/ as sysdba"SQL> SHUTDOWN IMMEDIATE;SQL> START ...
- Vue-设置默认路由选中
需求分析: 一个导航组件,需要其中一个是选中状态,并且样式呈现高亮,选中的导航对应的页面也需要展示出来. 功能实现: router-link内置有一个选中状态,当处于当前路由时,会给 router-l ...
- 类型不匹配 java.lang.IllegalArgumentException : argument type mismatch
异常: 解决: money的类型是 float类型(把0.8改成 0.8f 即可)
- 让你的逼格瞬间提升的十个Python语法!
Python 是一种代表简单思想的语言,其语法相对简单,很容易上手.不过,如果就此小视 Python 语法的精妙和深邃,那就大错特错了.本文精心筛选了最能展现 Python 语法之精妙的十个知识点,并 ...
- Python:re 模块
re模块是python内置的正则表达式模块
- Linux下运行SuperSocket记录
Linux下运行SuperSocket程序步骤 需要将原程序的配置文件中的内容拷贝一份,改名成SuperSocket.SocketService.exe.config1.Linux中运行.Net程 ...
- vue.js 强行赋值、刷新数组或者对象 方法之 $.set()
实际开发过程中,数据交互的的时候需要赋值,刷新,但是不可能每次赋值之后都刷新整个页面,所以就要用到 vue.js方法 $.set(),能实现赋值对象的局部刷新 语法:Vue.set(object, k ...
- Python笔记4
JSON: JavaScript Object Notation, JavaScript 对象标记 JSON 本质:是一种轻量级的数据交换格式 1. 轻量级 是 和 XML作比较 2. 数据交换格式 ...