Mybatis学习笔记(四)——MyBatis整合spring
来源:互联网 发布:好听的名字 知乎 编辑:程序博客网 时间:2024/06/06 01:29
本案例源码下载
导入jar包
新建一个java project,命名为mybatis-spring.
在项目下新建一个lib文件夹,导入相应的包:
导入后,选中lib下的所有jar包,右键——>Build Path——>Add to BuildPath.
src下新建sqlMapConfig.xml
sqlMapConfig.xml文件完整代码如下:
<?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> <!-- 设置别名 --> <typeAliases> <!-- 指定扫描包,会把包内所有的类都设置别名,别名的名称就是类名,大小写不敏感 --> <package name="com.zrxjuly.mybatis.pojo" /> </typeAliases> <mappers> <!-- 扫描mapper下的xml文件 --> <package name="com.zrxjuly.mybatis.mapper"/> </mappers></configuration>
src下新建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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 加载数据库配置文件. --> <context:property-placeholder location="classpath:db.properties"/> <!-- 配置数据库连接池 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <!-- 此处的property的name值不可随便更改,必须是以下名称 --> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> <property name="maxActive" value="10"></property> <property name="maxIdle" value="5"></property> </bean> <!-- mybatis的工厂. 此处的class名直接从 mybatis-spring-xxx.jar下的org.mybatis.spring下拷贝SqpSessionFactoryBean的类名称--> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入数据库连接池 --> <property name="dataSource" ref="dataSource"></property> <!-- mybatis核心配置文件的位置 --> <property name="configLocation" value="classpath:sqlMapConfig.xml"></property> </bean> <!-- 方式1:手动实例化DAO,原始的开发 --> <bean id="userDAO" class="com.zrxjuly.mybatis.dao.UserDAOImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactoryBean"></property> </bean> <!-- 方式2:使用Mapper动态代理开发 --> <!-- <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> 注入mybatis的工厂 <property name="sqlSessionFactory" ref="sqlSessionFactoryBean" /> <property name="mapperInterface" value="com.zrxjuly.mybatis.mapper.UserMapper"></property> </bean> --> <!-- 方式3:扫描 Mapper动态代理开发 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 基本包 扫描的是基础包下的类文件.--> <property name="basePackage" value="com.zrxjuly.mybatis.mapper"></property> </bean></beans>
src下的db.properties
jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8jdbc.username=rootjdbc.password=rongxiang
src下的log4j.properties
# Global logging configurationlog4j.rootLogger=ERROR, stdout # MyBatis logging configuration...log4j.logger.org.mybatis.example.BlogMapper=TRACE# Console output...log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
User.java
src下新建包,名为com.zrxjuly.mybatis.pojo,在该包下新建User.java
package com.zrxjuly.mybatis.pojo;import java.util.Date;public class User { private Integer id; private String username; private String sex; private Date birthday; private String address; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", sex=" + sex + ", birthday=" + birthday + ", address=" + address + "]"; }}
UserMapper.java
src下新建包,名为com.zrxjuly.mybatis.mapper,包下新建UserMapper.java。
package com.zrxjuly.mybatis.mapper;import com.zrxjuly.mybatis.pojo.User;public interface UserMapper { public User selectUserById(Integer id);}
UserMapper.xml
与UserMapper.java同一个目录下,新建UserMapper.xml文件
<?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.zrxjuly.mybatis.mapper.UserMapper"> <select id="selectUserById" parameterType="Integer" resultType="User"> select * from user where id=#{id} </select></mapper>
新建测试类
src下新建包,名为com.zrxjuly.mybatis.junit,在包下新建UserTest.java
package com.zrxjuly.mybatis.junit;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.zrxjuly.mybatis.mapper.UserMapper;import com.zrxjuly.mybatis.pojo.User;/** * * 测试类. * */public class UserTest { // 使用Mapper动态代理开发applicationContext.xml中的方式2. @SuppressWarnings("resource") @Test public void testMapper() { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); UserMapper userMapper = (UserMapper) ac.getBean("userMapper"); User user = userMapper.selectUserById(1); System.out.println(user); } // 使用Mapper动态代理开发applicationContext.xml中的方式3. @SuppressWarnings("resource") @Test public void testMappers() { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); UserMapper userMapper = (UserMapper) ac.getBean(UserMapper.class); User user = userMapper.selectUserById(1); System.out.println(user); }}
阅读全文
0 0
- Mybatis学习笔记(四)——MyBatis整合spring
- myBatis学习笔记(9)——spring整合mybatis
- Spring与MyBatis整合(学习笔记)
- mybatis学习笔记(十一)spring与mybatis整合
- MyBatis学习笔记(十二)spring与Mybatis整合事务
- Spring与mybatis整合---Mybatis学习笔记(十一)
- Spring与mybatis整合---Mybatis学习笔记(十一)
- spring mvc与Mybatis整合——(四)mvc与mybatis整合
- Mybatis框架学习(四)—查询缓存与spring的整合开发
- mybatis学习(五)--spring整合mybatis
- Mybatis学习日记(四) --整合spring,struts2
- Mybatis笔记之四 -- mybatis与spring整合简单例子
- Spring(四)Spring整合Mybatis
- MyBatis框架的学习(六)——MyBatis整合Spring
- Mybatis入门学习四:Spring 与 MyBatis整合
- mybatis-spring整合笔记
- Spring注解整合(hibernate+mybatis) 学习笔记
- Spring+Mybatis整合学习
- 构建基于Spark的推荐引擎(Python)
- 根据经纬度之间的值算距离
- solrCloud的安装和部署&新建集群
- dubbo 配置文件详解
- Unity中动态修改Terrain地形
- Mybatis学习笔记(四)——MyBatis整合spring
- 常用Linux操作命令
- python3.6的一些新功能
- 二维数组,根据二维数组内的元素进行排序
- GO 语言websocket编程
- rsync+sshpass使用
- Jquery 中的checkbox checked不显示问题
- Python之——实现SSH隧道功能
- 94. Binary Tree Inorder Traversal