内部邀请码:C8E245J (不写邀请码,没有现金送)
国内私募机构九鼎控股打造,九鼎投资是在全国股份转让系统挂牌的公众公司,股票代码为430719,为“中国PE第一股”,市值超1000亿元。
在搭建项目之前,创建数据库,以及数据库表,我用的数据库是mysql 5.6.11
创建数据库语句为:
- CREATE TABLE `orders` (
- `id` bigint(20) NOT NULL AUTO_INCREMENT,
- `company_id` varchar(255) DEFAULT NULL,
- `charge` decimal(18,4) NOT NULL DEFAULT '0.0000',
- `state` int(11) DEFAULT '0',
- `remark` varchar(500) DEFAULT NULL,
- PRIMARY KEY (`id`)
- );
搭建项目需要以下的包,
mybatis-3.2.3-SNAPSHOT.jar
mybatis-spring-1.2.0.jar
mysql-connector-java-5.1.24-bin.jar
c3p0-0.9.1.2.jar
commons-logging-1.1.1.jar
spring-aop-3.2.2.RELEASE.jar
spring-aspects-3.2.2.RELEASE.jar
spring-beans-3.2.2.RELEASE.jar
spring-context-3.2.2.RELEASE.jar
spring-context-support-3.2.2.RELEASE.jar
spring-core-3.2.2.RELEASE.jar
spring-expression-3.2.2.RELEASE.jar
spring-jdbc-3.2.2.RELEASE.jar
spring-tx-3.2.2.RELEASE.jar
好了,简单的准备工作已经准备就绪,现在来创建一个java项目或者java web项目
首先,创建dao接口类。
- package com.yunix.dao;
- public interface IOrderDao {
- public int getOrderCount();
- }
然后在com.yunix.dao.impl包下创建OrderDaoMapper.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="com.yunix.dao.IOrderDao">
- <select id="getOrderCount" resultType="int">
- <![CDATA[
- select count(1) from orders
- ]]>
- </select>
- </mapper>
其中namespace为dao接口,id为dao接口中的方法。
创建service接口:
- package com.yunix.service;
- public interface IOrderService {
- public int getOrderCount();
- }
创建service实现类:
- package com.yunix.service.impl;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import com.yunix.dao.IOrderDao;
- import com.yunix.service.IOrderService;
- @Service
- public class OrderService implements IOrderService {
- @Autowired
- private IOrderDao orderDao;
- @Override
- public int getOrderCount() {
- return orderDao.getOrderCount();
- }
- }
在src文件夹下创建applicationCntext.xml文件
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- default-autowire="byName"
- 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-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd">
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
- destroy-method="close">
- <property name="driverClass" value="com.mysql.jdbc.Driver" />
- <property name="jdbcUrl" value="jdbc:mysql://192.168.1.5:3306/test" />
- <property name="user" value="yunix" />
- <property name="password" value="123456" />
- <property name="initialPoolSize" value="1" />
- <property name="minPoolSize" value="0" />
- <property name="maxPoolSize" value="10" />
- <property name="acquireIncrement" value="5" />
- <property name="maxIdleTime" value="10" />
- <property name="maxStatements" value="0" />
- </bean>
- <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
- <constructor-arg index="0" ref="sqlSessionFactory" />
- </bean>
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="mapperLocations"
- value="classpath*:com/yunix/dao/impl/*Mapper.xml" />
- </bean>
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.yunix.dao" />
- </bean>
- <context:component-scan base-package="com.yunix.service" />
- </beans>
以及log4j.properties
- log4j.appender.stdout=org.apache.log4j.ConsoleAppender
- log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
- log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] -%m%n
- log4j.logger.com.ibatis=debug
- log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug
- log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug
- log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug
- log4j.logger.java.sql.Connection=debug
- log4j.logger.java.sql.Statement=debug
- log4j.logger.java.sql.PreparedStatement=debug,stdout
至此,项目搭建完成。
到底搭建成不成功,写一个测试类进行简单测试一下。我写的比较简单,写了个main方法进行了测试,测试代码如下:
- package test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import com.yunix.service.IOrderService;
- public class MyBatisTest {
- public static void main(String[] args) {
- @SuppressWarnings("resource")
- ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
- IOrderService orderService = (IOrderService)context.getBean("orderService");
- int count = orderService.getOrderCount();
- System.out.println("count:" + count);
- }
- }
运行结果为:
- count:11