网站建设与运营的市场,莒南县建设局网站,舒城网站建设,网页制作软件coreldraw总体流程
1.根据xml配置文件(全局配置文件)创建一个SqlSessionFactory对象2.sql映射文件#xff1b;配置了每一个sql#xff0c;以及sql的封装规则等3.将sql映射文件注册在全局配置文件中4.写代码1)根据全局配置文件得到SqlSessionFactory2)使用sqlSession工厂#xff0c;获…总体流程
1.根据xml配置文件(全局配置文件)创建一个SqlSessionFactory对象2.sql映射文件配置了每一个sql以及sql的封装规则等3.将sql映射文件注册在全局配置文件中4.写代码1)根据全局配置文件得到SqlSessionFactory2)使用sqlSession工厂获取到sqlSession对象使用他来执行增删改查一个sqlSession就是代表和数据库的一次会话用完关闭3)使用sql的唯一标志来告诉MyBatis执行哪个sqlsql都是保存在sql映射文件中
maybatis 依赖
dependencygroupIdorg.mybatis/groupIdartifactIdmybatis/artifactIdversionx.x.x/version
/dependency
mybatis 配置文件:
?xml version1.0 encodingUTF-8 ?
!DOCTYPE configurationPUBLIC -//mybatis.org//DTD Config 3.0//ENhttp://mybatis.org/dtd/mybatis-3-config.dtd
configurationenvironments defaultdevelopmentenvironment iddevelopmenttransactionManager typeJDBC/dataSource typePOOLEDproperty namedriver value${driver}/property nameurl value${url}/property nameusername value${username}/property namepassword value${password}//dataSource/environment/environments!--将我们写好的sql映射文件注册到全局配置文件中--mappersmapper resourceorg/mybatis/example/BlogMapper.xml//mappers
/configuration
根据xml配置文件(全局配置文件)创建一个SqlSessionFactoryBuilder对象
public SqlSessionFactory getSqlSessionFactory() throws IOException {String resource org/mybatis/example/mybatis-config.xml;
InputStream inputStream Resources.getResourceAsStream(resource);
return new SqlSessionFactoryBuilder().build(inputStream);}获取sqlSession对象实例能直接执行已经映射的sql语句 //1.获取sqlSessionFactory对象SqlSessionFactory sqlSessionFactory getSqlSessionFactory();//2.获取sqlSession对象SqlSession sqlSession sqlSessionFactory.openSession();sql映射文件:
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//EN
http://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespaceorg.mybatis.example.BlogMapper
select idselectBlog resultTypeBlog
select * from Blog where id #{id}
/select
/mapper
try {Blog blog sqlSession.selectOne(org.mybatis.example.BlogMapper.selectBlog, 101);
}finally
{
sqlSession.close();
}
如果数据库和实体类的数据名称不一样可以使用起别名的方法
select idselectBlog resultTypeBlog
select id,last_name lastName,email,gender from Blog where id #{id}
/select