怎么用mybatis去访问数据库了
1个回答
展开全部
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();
}
< 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();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询