利用Mybatis插入数据到MySQL中
来源:互联网 发布:centos arp清理 编辑:程序博客网 时间:2024/04/30 12:00
一、需要导入的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; }
阅读全文
0 0
- 利用Mybatis插入数据到MySQL中
- mybatis 插入中文数据到MySQL 乱码
- mybatis中批量插入数据(MYSQL)
- mybatis 插入数据到mysql数据库中返回id的方法
- spark (scala)使用mybatis插入数据到mysql
- MyBatis批量插入数据(MySql)
- MyBatis批量插入数据(MySql)
- 利用DataTable一次性将数据插入到数据库中
- 正确利用php向MySQL datetime类型中插入数据
- 正确利用php向MySQL datetime类型中插入数据
- ssh框架中插入数据到mysql出现乱码
- ssh框架中插入数据到mysql出现乱码
- ssh框架中插入数据到mysql出现乱码
- 用PHP批量插入数据到MySQL中
- Java 批量插入数据到数据库(MySQL)中
- C/C++插入数据到Mysql数据库中
- hibernate插入数据到mysql数据库中乱码问题
- python插入数据到mysql中乱码问题
- linux系统的启动顺序
- 练习3
- CCF 201709-01打酱油
- Linux中pthread_create传递参数的若干情况
- 如果你不小心僅了我的博客,發一下大佬的博客鏈接(傳送門)
- 利用Mybatis插入数据到MySQL中
- 前端之页面模块
- github上的html查看效果
- vue2.0+node.js+MongoDB全栈打造商城
- test3
- GTK实现:俄罗斯方块小游戏源代码(RussiaCube.c)
- 皮尔逊相关度欧式距离以及曼哈顿距离
- 转载:ICCV研讨会:实时SLAM的未来以及深度学习与SLAM的比较
- 练习4