mybatis 怎么创建数据库
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis 怎么创建数据库相关的知识,希望对你有一定的参考价值。
参考技术A 1,首先在包下创建Configuration.xml文件,该文件的格式如下:< xml version="1.0" encoding="UTF-8" > <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" ""> <configuration> <environments default="dev"> <environment id="dev"> <transactionManager type="JDBC"$amp;>amp;$lt;/transactionManager> <dataSource type="POOLED"> <property name="driver" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@192.168.1.20:1521:oa" /> <property name="username" value="zhangsan" /> <property name="password" value="123" /> </dataSource> </environment> </environments> <mappers> <mapper resource="com/cissst/oa/data/UserMapper.xml" /> <mapper resource="com/cissst/oa/data/DepartmentMapper.xml" /> </mappers> </configuration>
2,使用myBatis提供的工具类中的方法,从类路径或Configuration.xml文档所在位置加载资源文件。
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = null;
// 获取SqlSessionFactory对象
try
Reader reader = Resources
.getResourceAsReader("com/cissst/oa/data/Configuration.xml");
factory = builder.build(reader);
catch (Exception e)
e.printStackTrace();
3,使用工厂对象获取SqlSession 对象
SqlSession session=factory .openSession(false);
参数如果为true,表示该会话具有自动提交事务的功能,否则需程序员手动提交事务。
4,使用进行数据库访问
SqlSession session = super.getSqlSession();
// 构造返回值集合
List<UserEntity> result = new ArrayList<UserEntity>();
try
// 获取映射接口
UserMapper userMapper = session.getMapper(UserMapper.class);
// 调用接口中的方法
List<UserEntity> list = userMapper.getUserList(userEntity);
// 提交事务
session.commit();
catch (Exception e)
// 回滚事务
session.rollback();
finally
// 关闭会话
session.close();
本回答被提问者采纳
mybatis获取一个list怎么添加到数据库里
思路为采用Oracle 中insert语句的高级用法:INSERT ALL ,批量插入数据:
INSERT ALL举例:
1、建测试表
CREATE TABLE EDW_INT(
AGMT_NO VARCHAR2(40 BYTE) NOT NULL,
AGMT_SUB_NO VARCHAR2(4 BYTE) NOT NULL,
NEED_REPAY_INT NUMBER(22,2),
CURR_PERIOD NUMBER(4) NOT NULL
);
CREATE TABLE EDW_INT_1
(
AGMT_NO VARCHAR2(40 BYTE) NOT NULL,
AGMT_SUB_NO VARCHAR2(4 BYTE) NOT NULL,
NEED_REPAY_INT NUMBER(22,2),
CURR_PERIOD NUMBER(4) NOT NULL
);
CREATE TABLE EDW_INT_2
(
AGMT_NO VARCHAR2(40 BYTE) NOT NULL,
AGMT_SUB_NO VARCHAR2(4 BYTE) NOT NULL,
NEED_REPAY_INT NUMBER(22,2),
CURR_PERIOD NUMBER(4) NOT NULL
);
2.插入测试数据
INSERT INTO EDW_INT(AGMT_NO, AGMT_SUB_NO, NEED_REPAY_INT, CURR_PERIOD)
VALUES
('20003874', '2104', 3126.5, 7);
INSERT INTO EDW_INT
(AGMT_NO, AGMT_SUB_NO, NEED_REPAY_INT, CURR_PERIOD)
VALUES
('20003874', '2104', 3290.76, 6);
INSERT INTO EDW_INT
(AGMT_NO, AGMT_SUB_NO, NEED_REPAY_INT, CURR_PERIOD)
VALUES
('20003874', '2104', 3454.06, 5);
INSERT INTO EDW_INT
(AGMT_NO, AGMT_SUB_NO, NEED_REPAY_INT, CURR_PERIOD)
VALUES
('20003874', '2104', 3616.41, 4);
INSERT INTO EDW_INT
(AGMT_NO, AGMT_SUB_NO, NEED_REPAY_INT, CURR_PERIOD)
VALUES
('20017143', '2104', 2350.86, 0);
INSERT INTO EDW_INT
(AGMT_NO, AGMT_SUB_NO, NEED_REPAY_INT, CURR_PERIOD)
VALUES
('20017143', '2104', 3566.55, 0);
INSERT INTO EDW_INT
(AGMT_NO, AGMT_SUB_NO, NEED_REPAY_INT, CURR_PERIOD)
VALUES
('20018273', '2104', 1639.46, 0);
INSERT INTO EDW_INT
(AGMT_NO, AGMT_SUB_NO, NEED_REPAY_INT, CURR_PERIOD)
VALUES
('20018273', '2104', 2080.49, 0);
COMMIT;
3.insert all-不带条件
insert allinto edw_int_1(agmt_no,agmt_sub_no,need_repay_int,curr_period)
values(agmt_no,agmt_sub_no,need_repay_int,curr_period)
into edw_int_2(agmt_no,agmt_sub_no,curr_period)
values(agmt_no,'1234',curr_period)
select agmt_no,agmt_sub_no,need_repay_int,curr_period from edw_int;
commit;
结合该问题具体分析:
mybatis 的和dao相关的sql写法如下:
INSERT ALL
<foreach collection="list" item="item">
INTO FSP_BUSINESS_INTEREST_REPORT
(
ID,
BUSINESS_DAY,
LOAN_NO,
CIF_NO,
CIF_NAME,
LOAN_TYPE,
REPAY_WAY,
TERM_TYPE,
VOUCH_WAY,
CHANNEL_TYPE,
LOAN_BEGIN_DATE,
LOAN_END_DATE,
RATE_BEGIN_DATE,
RATE_END_DATE,
AMT,
RATE_TYPE,
RATE,
RATE_AMT,
PAY_TYPE,
REPORT_OUT_FLAG,
REPORT_OUT_DATE,
REPORT_OUT_AMT,
CREATOR,
CREATE_TIME,
UPDATOR,
UPDATE_TIME
) VALUES (
getSeqByName('SEQ_FSP_BUS_INT_REPORT'),
#businessDay, jdbcType=VARCHAR,
#loanNo, jdbcType=VARCHAR,
#cifNo, jdbcType=VARCHAR,
#cifName, jdbcType=VARCHAR,
#loanType, jdbcType=VARCHAR,
#repayWay, jdbcType=VARCHAR,
#termType, jdbcType=VARCHAR,
#vouchWay, jdbcType=VARCHAR,
#channelType, jdbcType=VARCHAR,
#loanBeginDate, jdbcType=DATE,
#loanEndDate, jdbcType=DATE,
#rateBeginDate, jdbcType=DATE,
#rateEndDate, jdbcType=DATE,
#amt, jdbcType=NUMERIC,
#rateType, jdbcType=VARCHAR,
#rate, jdbcType=NUMERIC,
#rateAmt, jdbcType=NUMERIC,
#payType, jdbcType=VARCHAR,
#reportOutFlag, jdbcType=VARCHAR,
#reportOutDate, jdbcType=DATE,
#reportOutAmt, jdbcType=NUMERIC,
#creator, jdbcType=VARCHAR,
#createTime, jdbcType=DATE,
#updator, jdbcType=VARCHAR,
#updateTime, jdbcType=DATE
)
</foreach>
SELECT 1 FROM DUAL
</insert>
2.dao中接口如下定义:
以上是关于mybatis 怎么创建数据库的主要内容,如果未能解决你的问题,请参考以下文章
idea中怎么整合springspringmvcmybatis
mybatis的逆向工程中生成的example文件是怎么使用的