mybatis+spring maven下 整合 完整demo

<span style="font-family: Arial, Helvetica, sans-serif;">当前项目代码地址</span>
<pre code_snippet_id="429127" snippet_file_name="blog_20140717_1_9741099" name="code" class="csharp">http://download.csdn.net/detail/philip502/7646963
 
 
</pre><pre code_snippet_id="429127" snippet_file_name="blog_20140717_2_8966541" name="code" class="csharp">pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>javamavenstudy</groupId>
	<artifactId>spring-mybatis-demo-01</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>spring-mybatis-demo-01</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<org.springframework.version>4.0.2.RELEASE</org.springframework.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>4.0.2.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.2.5</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.2.2</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.29</version>
		</dependency>

	</dependencies>
</project>

jobdriverlog.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="spring.mybatis.demo01.IJobDriverLogDao">

	<resultMap type="spring.mybatis.demo01.JobDriverLog" id="logResult">

		<result property="id" column="id" jdbcType="INTEGER" javaType="java.lang.Integer" />
		<result property="job_code" column="job_code" />
		<result property="status" column="status" />
		<result property="start_time" column="start_time" />
		<result property="end_time" column="end_time" />
		<result property="comment" column="comment" />
		<result property="create_time" column="create_time" />
		<result property="data_begin_time" column="data_begin_time" />
		<result property="data_end_time" column="data_end_time" />

	</resultMap>
	<select id="fetch" parameterType="JobDriverLog" resultMap="logResult">
	<![CDATA[
		select * from job_driver_log
		where
		job_code = #{job_code} and data_begin_time <= #{data_begin_time} and data_end_time >= #{data_end_time}
		]]>
	</select>

	<select id="insert" parameterType="JobDriverLog" resultMap="logResult">
	<![CDATA[
		insert into job_driver_log (job_code,status,start_time,end_time,comment,data_begin_time,data_end_time,create_time)
		values(#{job_code},#{status},#{start_time},#{end_time},#{comment},#{data_begin_time},#{data_end_time},#{create_time})
		]]>
	</select>

</mapper>

beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" 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/jdbc
	http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd">
	<context:annotation-config />
	<context:component-scan base-package="*" />


	<bean id="rpbgDataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://10.3.255.21:3320/tc" />
		<property name="username" value="off_mynet" />
		<property name="password" value="9x7c5bMox3Vq" />
	</bean>

	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="rpbgDataSource" />
		<property name="configLocation" value="mybatis.xml" />
	</bean>

</beans>

IJobDriverLogDao.java

/**
 * 
 */
package spring.mybatis.demo01;


/**
 * This class is used for ...
 * 
 * @author luyifeng
 * @version 1.0, 2014-7-16 下午7:10:31
 */
public interface IJobDriverLogDao {
	
	public abstract JobDriverLog fetch(JobDriverLog log);

	public abstract void insert(JobDriverLog log);
}

JobDriverLog.java

/**
 * 
 */
package spring.mybatis.demo01;

/**
 * This class is used for ...
 * 
 * @author luyifeng
 * @version 1.0, 2014-7-16 下午5:12:37
 */
public class JobDriverLog {
	private int id;
	private String job_code;
	private int status;
	private int start_time;
	private int end_time;
	private String comment;
	private int create_time;
	private int data_begin_time;
	private int data_end_time;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getJob_code() {
		return job_code;
	}

	public void setJob_code(String job_code) {
		this.job_code = job_code;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public int getStart_time() {
		return start_time;
	}

	public void setStart_time(int start_time) {
		this.start_time = start_time;
	}

	public int getEnd_time() {
		return end_time;
	}

	public void setEnd_time(int end_time) {
		this.end_time = end_time;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public int getCreate_time() {
		return create_time;
	}

	public void setCreate_time(int create_time) {
		this.create_time = create_time;
	}

	public int getData_begin_time() {
		return data_begin_time;
	}

	public void setData_begin_time(int data_begin_time) {
		this.data_begin_time = data_begin_time;
	}

	public int getData_end_time() {
		return data_end_time;
	}

	public void setData_end_time(int data_end_time) {
		this.data_end_time = data_end_time;
	}

}

App.java

package spring.mybatis.demo01;

import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Hello world!
 * 
 */
public class App {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext(
				"config/beans.xml");
		SqlSessionFactory factory = (SqlSessionFactory) ac
				.getBean("sqlSessionFactory");
		System.out.println(factory);
		System.out.println("Hello World!");
	}
}

最后的目录结构图:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码者人生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值