简单的mybatis——spring 整合(数据库:oracle)
刚刚开始学,mybatis,怕自己忘记基础,赶紧记录了下来,欢迎各位大神指错!
mybatis所需的jar包:mybatis-3.4.2.jar
mybatis与spring整合中需要的jar包:mybatis-spring-1.3.1.jar
其他jar包就找自己的需求来添加:比如oracle的驱动包,spring需要的相应的包
注:虽然配置文件里写了:增删改查,但是测试只测试查询所有,其他的自己进行测试了哈
大概思路:
首先:创建数据,建表,插入测试数据
再而:创建与表相对应的实体类
下一步:在接口中定义一些数据访问的方法
进一步:为MyBatis ORM创建的xml映射文件定义实现数据访问需要的sql脚本
最后:使用spring.xml完成整合,进行小测试
建表,插数据
CREATE TABLE books(bid NUMBER PRIMARY KEY ,bname VARCHAR2(50) NOT NULL ,bprice NUMBER NOT NULL ); INSERT INTO books(bid,bname,bprice) VALUES (1,'百鬼夜行',12.5); INSERT INTO books(bid,bname,bprice) VALUES (2,'陌上花开',22.5); INSERT INTO books(bid,bname,bprice) VALUES (3,'夏目友人帐',32.5);
创建实体类
package edu.nf.mybatis.entity; /** * Created by Administrator on 2017/3/27. */ public class Books { private long bid; private String bname; private double bprice; public Books() { } public Books(long bid, String bname, double bprice) { this.bid = bid; this.bname = bname; this.bprice = bprice; } public long getBid() { return bid; } public void setBid(long bid) { this.bid = bid; } public String getBname() { return bname; } public void setBname(String bname) { this.bname = bname; } public double getBprice() { return bprice; } public void setBprice(double bprice) { this.bprice = bprice; } @Override public String toString() { return "Books{" + "bid=" + bid + ", bname='" + bname + '\'' + ", bprice=" + bprice + '}'; } }
定义访问接口
package edu.nf.mybatis.mapper; import edu.nf.mybatis.entity.Books; import org.apache.ibatis.annotations.Param; import java.util.List; /** * Created by Administrator on 2017/3/27. * 图书数据访问的接口 */ public interface IBookDao { /*获取所有书籍*/ public List<Books> getAll(); /*通过ID获取单个数据*/ public Books getBookById(@Param("bid") int bid); /*添加数据*/ public int add(Books entity); /*根据ID删除*/ public int del(int bid); /*更新*/ public int update(Books entity); }
创建xml映射文件定义实现数据访问需要的sql脚本
<?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="edu.nf.mybatis.mapper.IBookDao"> <!--id应该是对应接口的包名+接口名--> <!--获取所有书籍--> <!--因为在spring.xml文件中的sqlSessionFactory中配置属性 <property name="typeAliasesPackage" value="edu.nf.mybatis.entity"></property>--> <!--所以这里resultType="Books"就只需要写类名Books,而不是edu.nf.mybatis.entity.Books了--> <select id="getAll" resultType="Books"> select bid,bname,bprice from books </select> <!--通过编号获取书籍--> <select id="getBookById" resultType="Books"> select bid,bname,bprice from books where bid = #{bid}; </select> <!--添加书籍--> <insert id="add"> insert into books(bid,bname,bprice) values (#{bid},#{bname},#{bprice}); </insert> <!--删除书籍--> <delete id="del"> delete from books where bid = #{bid}; </delete> <!--更新书籍--> <update id="update"> update books set bid = #{bid},bname = #{bname},bprice = #{bprice} where bid = #{bid}; </update> </mapper>
spring.xml完成整合,进行小测试
<?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/tx" 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-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <!--1 引入属性文件,在配置中占位使用 --> <context:property-placeholder location="classpath:db.properties" /> <!--6 容器自动扫描IOC组件 --> <context:component-scan base-package="edu.nf.mybatis"/> <!--2 配置C3P0数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <!--驱动类名 --> <property name="driverClass" value="${driver}" /> <!-- url --> <property name="jdbcUrl" value="${url}" /> <!-- 用户名 --> <property name="user" value="${user}" /> <!-- 密码 --> <property name="password" value="${password}" /> </bean> <!--3 会话工厂bean sqlSessionFactoryBean --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 数据源 --> <property name="dataSource" ref="dataSource"></property> <!-- 配置这里后在bookMapper.xml文件中,sql语句返回的类型就不许写全路径,直接写类名就好了--> <property name="typeAliasesPackage" value="edu.nf.mybatis.entity"></property> <!-- sql映射文件路径 --> <property name="mapperLocations" value="classpath:mybatis/*.xml"></property> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 指定要自动扫描接口的基础包,实现接口 --> <property name="basePackage" value="edu.nf.mybatis.mapper" /> </bean> <!--5 声明式事务管理 --> <!--定义事物管理器,由spring管理事务 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--支持注解驱动的事务管理,指定事务管理器 --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
import edu.nf.mybatis.entity.Books; import edu.nf.mybatis.sevice.BooksService; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.List; /** * Created by Administrator on 2017/3/27. */ public class testMybatis { @Test public void test2(){ ApplicationContext ctx=new ClassPathXmlApplicationContext("spring.xml"); BooksService booksService=ctx.getBean(BooksService.class); List<Books> books=booksService.getAll(); System.out.println(books); } }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置路径,默认是 "WEB-INF/applicationContext.xml" --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param> <servlet-mapping> <servlet-name>myBatis</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
test2测试运行结果: