MybatisPlus + Spring 的实现 对Mybatis 进行简化

代码结构:

applicationContext.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:lang="http://www.springframework.org/schema/lang" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd">
     

   <!-- c3p0连接池 -->
  <context:property-placeholder location="db.properties"/>
  <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
   <!-- 配置数据库信息 -->
				<property name="driverClass" value="${mysql.driver}" />
				<property name="jdbcUrl" value="${mysql.url}" />
				<property name="user" value="${mysql.username}" />
				<property name="password" value="" />
  </bean>


   <!-- 事务管理器 -->
   <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
   <property name="dataSource" ref="dataSource"></property>
   </bean>	
   
   <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
   
   <!--SqlSessionFactoryBean  -->
   <!--mybatisSqlSessionFactoryBean 是mybatisPlus的方式  -->
   <bean id="sqlSessionFactoryBean" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
   <property name="dataSource" ref="dataSource"></property>
    <property name="configLocation" value="mybatis.xml"></property>
   <property name="typeAliasesPackage" value="com.cjr"></property>
   </bean>
   <!--mapper  -->
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.mapper"></property>
   </bean>
   
   </beans>

mybatis.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>


<properties resource="db.properties"></properties>

<!--开启日志 -->
	<settings>
		<setting name="logImpl" value="LOG4J" />
		
	</settings>

</configuration>

StudentMapper.java

package com.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cjr.student;

//继承BaseMapper 可直接进行接口的实现,省略Mapper的步骤
public interface StudentMapper extends BaseMapper<student>{
	

}

student.java

package com.cjr;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

@TableName("student")
public class student {
	@TableId(value="stuNo", type=IdType.AUTO)
	private int stuNo;
	@TableField(value="stuName")
	private String stuName;
	@TableField(value="stuAge")
	private int stuAge;
	
	public student(int stuNo, String stuName, int stuAge) {
		super();
		this.stuNo = stuNo;
		this.stuName = stuName;
		this.stuAge = stuAge;
	}
	
	public student( String stuName, int stuAge) {

		this.stuName = stuName;
		this.stuAge = stuAge;
	}
	public student() {
		
	}
	public int getStuNo() {
		return stuNo;
	}
	public void setStuNo(int stuNo) {
		this.stuNo = stuNo;
	}
	public String getStuName() {
		return stuName;
	}
	public void setStuName(String stuName) {
		this.stuName = stuName;
	}
	public int getStuAge() {
		return stuAge;
	}
	public void setStuAge(int stuAge) {
		this.stuAge = stuAge;
	}

}

测试类:

package com.test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.cjr.student;
import com.mapper.StudentMapper;
import com.mchange.v2.c3p0.ComboPooledDataSource;

public class test {



	public static void testInsert() {
		ApplicationContext context =	new ClassPathXmlApplicationContext("applicationContext.xml");
	    //从ioc中获取mapper
		StudentMapper studentmapper = (StudentMapper)context.getBean("studentMapper");
	student	student =  new student("zs",20);
	   int count = studentmapper.insert(student);
	  // System.out.println(count);
	   List<student> list = studentmapper.selectList(null);
	   for(int i=0;i<list.size();i++)
	   System.out.println(list.get(i).getStuNo());
	}
	
	public static void main(String[] args) throws SQLException {
		
//		ApplicationContext context =	new ClassPathXmlApplicationContext("applicationContext.xml");
//	    ComboPooledDataSource ds = (ComboPooledDataSource) context.getBean("dataSource");
//    System.out.println("-------"+ds);
//    Connection  connection = ds.getConnection();
//    System.out.println("---------"+connection);
		
		testInsert();	
    
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值