利用Mybatis插入数据到MySQL中

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014785687/article/details/78219646

一、需要导入的jar包

         <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.3.11.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.3</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.18</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.14</version>
        </dependency>

二、数据库的设置和 SqlSession Bean

<bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:database.properties</value>

            </list>
        </property>
    </bean>

    <!--BI 商品推荐数据源-->
    <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" />

        <!--打开PSCache,并且指定每个连接上PSCache的大小-->
        <property name="poolPreparedStatements" value="false" />
        <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />

    </bean>



    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!--mybatis的配置文件-->
        <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的映射

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.jolly.spider.bean.supplierProductMapper" >


<!--返回自增的id-->
    <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;
    }
展开阅读全文

没有更多推荐了,返回首页