spring与mybatis整合
来源:互联网 发布:学生考试成绩分析软件 编辑:程序博客网 时间:2024/06/01 21:09
两种方式
3.DAO层实现类
4.spring配置文件
Mapper接口方式开发
2.映射文件
3mybatis.xml
4.spring的配置文件
注意:Mapper接口方式编程简单,但具体应用遵循规范:
1.传统DAO方式开发整合:
实体类:
package com.hwj.po;public class Customer { private int id; private String username; private String phone; 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 String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; }}2.DAO层
package com.hwj.dao;import com.hwj.po.Customer;import org.springframework.stereotype.Repository;@Repositorypublic interface CustomerDao { public Customer findCustomerById(Integer id);}
3.DAO层实现类
package com.hwj.dao;import com.hwj.po.Customer;import org.mybatis.spring.support.SqlSessionDaoSupport;public class CustomerDaoImpl extends SqlSessionDaoSupport implements CustomerDao{ public Customer findCustomerById(Integer id) { return this.getSqlSession().selectOne("CustomerMapper.findCustomerById",id); }}
4.spring配置文件
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 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"> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 配置连接池属性 --> <property name="driverClass" value="com.mysql.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/blog?useUnicode=true&characterEncoding=UTF-8" /> <property name="user" value="root" /> <property name="password" value="root" /> <!--看不懂这是什么!!大概是固定的吧!!--> <!-- c3p0连接池的私有属性 --> <property name="maxPoolSize" value="30" /> <property name="minPoolSize" value="10" /> <!-- 关闭连接后不自动commit --> <property name="autoCommitOnClose" value="false" /> <!-- 获取连接超时时间 --> <property name="checkoutTimeout" value="10000" /> <!-- 当获取连接失败重试次数 --> <property name="acquireRetryAttempts" value="2" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <bean id="customerDao" class="com.hwj.dao.CustomerDaoImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean></beans>5.mybatis配置文件
<?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.hwj.po"></package></typeAliases><mappers> <mapper resource="CustomerMapper.xml"/></mappers></configuration>6.映射文件
<?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="CustomerMapper"> <select id="findCustomerById" parameterType="Integer" resultType="customer"> select * from customer where id=#{id} </select></mapper>
Mapper接口方式开发
1.Mapper接口
package com.hwj.mapper;import com.hwj.po.Customer;public interface CustomerMapper { public Customer findCustomerById(Integer id);}
2.映射文件
<?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.hwj.mapper.CustomerMapper"> <select id="findCustomerById" parameterType="Integer" resultType="customer"> select * from customer where id=#{id} </select></mapper>
3mybatis.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.hwj.po"></package> </typeAliases> <mappers> <mapper resource="CustomerMapper.xml"/> </mappers></configuration>
4.spring的配置文件
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/cache" 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/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 配置连接池属性 --> <property name="driverClass" value="com.mysql.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/blog?useUnicode=true&characterEncoding=UTF-8" /> <property name="user" value="root" /> <property name="password" value="root" /> <!--看不懂这是什么!!大概是固定的吧!!--> <!-- c3p0连接池的私有属性 --> <property name="maxPoolSize" value="30" /> <property name="minPoolSize" value="10" /> <!-- 关闭连接后不自动commit --> <property name="autoCommitOnClose" value="false" /> <!-- 获取连接超时时间 --> <property name="checkoutTimeout" value="10000" /> <!-- 当获取连接失败重试次数 --> <property name="acquireRetryAttempts" value="2" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <bean id="customerMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.hwj.mapper.CustomerMapper"></property> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> </beans>
注意:Mapper接口方式编程简单,但具体应用遵循规范:
1.Mapeer接口的名称和对应的Mapper.xml映射文件的名称必须一致
2.Mapper.xml文件中的namespace与Mapper接口的类路径相同
还有!!
IDEA中不能把映射文件放到java包里面,必须要放到resources里面,不然找不到xml
阅读全文
0 0
- spring与mybatis整合
- 整合spring与myBatis
- mybatis 与 spring 整合
- mybatis与spring整合
- mybatis与spring整合
- mybatis与spring整合
- spring与mybatis整合
- spring与mybatis整合
- Spring与Mybatis整合
- Mybatis与Spring整合
- spring与mybatis整合
- mybatis与spring整合
- spring与mybatis整合
- Mybatis与Spring整合
- MyBatis与Spring整合
- Spring与MyBatis整合
- Spring与MyBatis整合
- mybatis与spring整合
- python异常定义与原理
- redis-springboot-redistemplate更改序列化方式
- Z10待机功耗分析(印度log20161207)
- CNN结构:Windows使用FasterRCNN-C++版本
- python 一些有用函数
- spring与mybatis整合
- 一个朋友圈泛型问题引发的“案子”
- Mysql 查询到的数据列以逗号方式隔开 返回
- Intellij IDEA安装golang插件
- Spring+ Spring cloud + SSO单点登录应用认证
- 区块链
- Spring加载资源文件的配置
- 程序员的鄙视链
- web.xml启动顺序