Spring中用Oracle

2024-01-03 10:52:20 浏览数 (3)

1.Oracle mybatis

2.从.properties配置文件到SqlSessionTemplate模板的建立

(1)jdbc.properties
代码语言:javascript复制
jdbc.driverClassName = oracle.jdbc.driver.OracleDriver
jdbc.url = jdbc:oracle:thin:xxxxxx
jdbc.username = xxxx
jdbc.password = xxxx
(2) pom.xml添加依赖
代码语言:javascript复制
<dependency>
	<groupId>com.oracle</groupId>
    <artifactId>ojdbc6</artifactId>
    <version>11.2.0.3</version>
</dependency>
<dependency>
	<groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.1</version>
</dependency>
<dependency>
	<groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.3.0</version>
</dependency>
(3)Spring配置类
代码语言:javascript复制
@Configuration
@PropertySource({"classpath:config.properties"})
@ComponentScan(basePackages = {"com.xxx.xx"})
@MapperScan(value = {"xxxxx","xxxxxxx"})
public class SpringConfig{
    
    @Bean
    public DataSource dataSource(Environment env){
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(env.getProperty("jdbc.dirverClassName"));
        dataSource.setUrl(env.getProperty("jdbc.url"));
        dataSource.setUsername(env.getProperty("jdbc.username"));
        dataSource.setPassword(env.getProperty("jdbc.password"));
        return dataSource;
    }
    
    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) thrwos IOException{
        SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource);
        sqlSessionFactory.setConfigLocation(
        	new PathMatchingResourcePatternResolver().getResource("classpath:mapper/configuration.xml"));
        sqlSessionFactory.setMapperLocations(
        	new PathMatchingResourcePatternResolver().getResource("classpath:mapper/*Mapper*.xml"));
        return sqlSessionFactory;
    }
    
    @Bean
    public SqlSessionTemplate sessionTemplate(SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }
    
    @Bean
    public JdbcTemplateFactory jdbcTemplateFactory(SqlSessionTemplate sqlSessionTemplate){
        JdbcTemplateFactory instance = JdbcTemplateFacotry.getInstance();
        instance.setSessionTemplate(sqlSessionTemplate);
        return instance;
    }
}
4.testMapper.xml
代码语言:javascript复制
<mapper namespace="xx.xx.xx.xx">
	<requestMap type="xx.xx.xx.xx.xxClass" id="map1">
    	<id column="XX_KEY" property="xxkey" />
        <result column="XXX_DATA1" property="xxxdata1" />
        <result column="XXX_DATA2" property="xxxdata2" />
    </requestMap>
	
    <select id="queryByxxxx" parameterType="String" resultMap="map1">
    	SELECT * FROM XXX WHERE XX_DATA1= #{xxxdata1}
    </select>
    
    <update id="updateByxxx" parameterType="mapxx">
    	UPDATE XXX SET DATA1=#{},DATA2=#{}
    </update>
</mapper>

3.用SqlSessionTemplate来实现dao层

String类型的key值——os加密——Long类型——toString

statement对应mapper.xml

代码语言:javascript复制
@Resource(name = "sessionTemplate")
private transient SqlSessionTemplate template;

public xx query(String statement,String key){
   try{
       SqlSession session = template.getSqlSessionFactory().openSession();
	   xx = session.selectOne(statement,key);
       xx = session.update(statement,param);
   }catch(Exception e){
       ...
   }finally{
       session.close();
   }
   return xx;
}

0 人点赞