在一个地方呆久了,就会慢慢的远离了外界。
我也是,一直在一个公司里面总是在使用的xml协议来处理数据层。所以最近自己学习了一下mybatis其官网是https://code.google.com/p/mybatis/
mybatis3+spring3
主要依赖:
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.1.1</version> </dependency>
spring的依赖为<springframework.version>3.1.1.RELEASE</springframework.version>
配置
<!-- 数据源配置1 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/payoffdatabase?useUnicode=true&characterEncoding=UTF-8" /> <property name="username" value="root" /> <property name="password" value="root" /> <property name="defaultAutoCommit" value="true"></property> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:ibatis/mybatis-config.xml" /> </bean> <bean id="mapperConfig" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.gozap.mapper"></property> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <tx:annotation-driven transaction-manager="transactionManager" />
之后简单的写了个mapper和service然后在单元测试中测试
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:/spring/*.xml" }) public class UserloginServiceTest { @Autowired private UserloginService service; @Test public void addTest() throws Exception { UserLogin userLogin = new UserLogin(); userLogin.setPassword("102"); userLogin.setUsername("102"); service.add(userLogin); } }
好运行ok(其他的无用代码就不在贴了)
学完了后又想现在的世界是大数据量的世界,所以如何使用多数据源来配置呢,请听下回讲解