一、需要导入的jar包
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>4.3.11.RELEASEversion>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.3.1version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>1.2.3version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.0.18version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.14version>
dependency>
二、数据库的设置和 SqlSession Bean
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:database.propertiesvalue>
list>
property>
bean>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="initialSize" value="${jdbc.initialPoolSize}" />
<property name="minIdle" value="${jdbc.minPoolSize}" />
<property name="maxActive" value="${jdbc.maxPoolSize}" />
<property name="maxWait" value="${jdbc.maxWaitTime}" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="validationQuery" value="SELECT 'x'" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="poolPreparedStatements" value="false" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-config.xml" />
<property name="dataSource" ref="dataSource" />
bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
bean>
三、mybatis配置文件
<configuration>
<mappers>
<mapper resource="supplierProductMapper.xml"/>
mappers>
configuration>
四、实体类
public class SupplierProduct {
private int id;
private int supplierId;
private String createdAt;
private String updatedAt;
private String deletedAt;
private int isDeleted;
//省略get、set方法
五、实体类与mybatis的映射
<mapper namespace="com.jolly.spider.bean.supplierProductMapper" >
<insert id="insert" useGeneratedKeys="true" keyProperty="id" parameterType="com.jolly.spider.bean.SupplierProduct">
INSERT INTO
supplier_product
(
supplier_id,created_at,updated_at,process_status,is_deleted,supp_parent_id,supp_parent_code,
name,
description,category,image_url,image_list,extra,currency
)VALUES(
#{supplierId},#{createdAt},#{updatedAt},#{processStatus},#{isDeleted},#{suppParentId},#{suppParentCode},#{name},
#{description},#{category},#{imageUrl},#{imageList},#{extra},#{currency}
)
insert>
mapper>
六、测试
private final static String SPACE="com.jolly.spider.bean.supplierProductMapper";
private static String path="database.config.xml";
private SqlSessionTemplate sqlSession;
public SupplierDao(){
ApplicationContext context=new ClassPathXmlApplicationContext(path);
sqlSession=(SqlSessionTemplate) context.getBean("sqlSession");
}
public int insert(SupplierProduct sp){
try{
return sqlSession.insert(SPACE+".insert",sp);
}catch (Exception e){
e.printStackTrace();
}
return -1;
}