mybatis + spring 的基本配置步骤
来源:互联网 发布:声音编辑软件 编辑:程序博客网 时间:2024/05/22 16:52
mybatis + spring 的基本配置步骤
1.Spring和MyBatis整合需要专门的jar库,可在pom.xml文件中依赖注入,注意,要在网络稳定下注入,否则可能导致无法使用,代码如下:
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version></dependency><dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version></dependency>
2.在mybatis-config.xml文件中连接LogicMapper.xml(文件名根据业务自行定义,内容主要写sql语句):
<mappers> <mapper resource="mappers/LogicMapper.xml"/> </mappers>
3.,在基本的 MyBatis 中,session 工厂可以使用 SqlSessionFactoryBuilder 来创建。而在 MyBatis-spring 中,则使用 SqlSessionFactoryBean 来替代。自定义jdbc-dao.xml文件如下,注释在文中,不多强调:
<!--创建SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource_proxool"></property> <!-- mybatis-config.xml是上文配置的,名称必须相同 --> <property name="configLocation" value="classpath:mybatis-config.xml"></property> <!-- 整合PageHelper分页插件,用户数据库sql查询时的分页处理,简化很多步骤 --> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageHelper"> <property name="properties"> <value> dialect=oracle </value> </property> </bean> </array> </property> </bean><!-- 定义基本的baaeMapper --> <bean id="baseMapper" class="org.mybatis.spring.mapper.MapperFactoryBean" abstract="true" lazy-init="true"> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean><!-- cn.ping.spring.mybatis.LogicMapper是自己定义的业务接口LogicMapper.java所在路径 --> <bean id="LogicMapper" parent="baseMapper"> <property name="mapperInterface" value="cn.ping.spring.mybatis.LogicMapper" /> </bean>
4、LogicMapper.xml,里面写sql 语句,这里以查询部门信息为例,namespace属性必须写,内容为LogicMapper接口的路径,和上面bean的value相同。
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="cn.ping.spring.mybatis.LogicMapper" > <!-- 在同一命名空间下面,id是不可以重复的 --> <select id="getDepts" resultType="java.util.Map"> select * from dept <where> <if test="dname != null"> and dname like #{dname} </if> <if test="loc != null"> and loc like #{loc} </if> </where> </select></mapper>
5、定义LogicMapper接口 ,getDepts为LogicMapper.xml文件中select语句的id,
public interface LogicMapper { List<Map<String,Object>> getDepts();}
6.建一个LogicTest.java类
public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("jdbc-dao.xml"); //获得LogicMapper接口 LogicMapper mapper = ac.getBean(LogicMapper.class); //分页插件的使用,第一页,选取5个数字,这里的内容自定义 PageHelper.startPage(1,5); //调用接口的方法,直接查出数据库的内容 List<Map<String,Object>> rows = mapper.getDepts(); //PageInfo可以打印出分页的所有信息,包括,起始页,总条数,总页数,等等信息 PageInfo<Map<String,Object>> pi = new PageInfo<Map<String,Object>>(rows); System.out.println("pi:"+pi); for (Map<String, Object> map : rows) { System.out.println(map); } }
7.查询结果如下图:
0 0
- mybatis + spring 的基本配置步骤
- mybatis整合Spring的基本步骤
- Mybatis-spring基本配置
- Spring+Mybatis基本配置。
- Spring+SpringMVC+mybatis的基本配置
- mybatis-mybatis与spring整合的基本配置
- Eclipse spring基本配置步骤
- Spring-Mybatis配置文件基本配置
- mybatis-mybatis的基本配置
- SSM(spring+springMVC+Mybatis)配置步骤
- Spring Boots &mybatis连接数据库配置步骤
- MyBatis的基本配置
- Mybatis的Spring配置
- 配置Struts+Hibernate+Spring基本步骤
- Spring Boot + Mybatis + Ehcache架构基本配置
- Spring Boot + Mybatis + Ehcache架构基本配置
- 【Spring+MyBatis+MySQL实战入门】三、MyBatis的4个基本配置介绍
- MyBatis基本使用步骤
- Android夜间模式切换的方法
- Zookeeper初学篇
- master and slave have equal MySQL server UUIDs问题解决
- DFS&BFS小结
- test
- mybatis + spring 的基本配置步骤
- (笔记)位运算
- 《Spring in Action》第四版第一章《将 Spring 付诸实践》读书笔记(一)
- Python+PyQt,GUI操作
- Android ButterKnife框架的使用
- Python打印某范围内的素数
- NOJ 1453 筛法
- 初识Hadoop之HDFS
- 由Docker开始畅想