0%
AccountServiceImpl.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| @Service("accountService") public class AccountServiceImpl implements AccountService { @Override public void save(Account account) { InputStream resourceAsStream = null; try { resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = build.openSession(true); AccountMapper mapper = sqlSession.getMapper(AccountMapper.class); mapper.save(account); } catch (IOException e) { e.printStackTrace(); } }
@Override public List<Account> findAll() { InputStream resourceAsStream = null; try { resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = build.openSession(true); AccountMapper mapper = sqlSession.getMapper(AccountMapper.class); List<Account> all = mapper.findAll(); return all; } catch (IOException e) { e.printStackTrace(); } return null; } }
|
applicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| <context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="maxTotal" value="${jdbc.maxTotal}"/> <property name="maxIdle" value="${jdbc.maxIdle}"/> <property name="initialSize" value="${jdbc.initialSize}"/> </bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="sqlMapConfig-spring.xml"/> </bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="mapper"/> </bean>
|
AccountServiceImpl.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| @Service("accountService") public class AccountServiceImpl implements AccountService {
@Autowired private AccountMapper accountMapper;
@Override public void save(Account account) { accountMapper.save(account); }
@Override public List<Account> findAll() { List<Account> all = accountMapper.findAll(); return all; } }
|