헤르메스 LIFE

[Spring] Spring + mySql 설정 본문

Spring Framework

[Spring] Spring + mySql 설정

헤르메스의날개 2012. 2. 23. 10:28
728x90
applicationContext-datasource.xml

<bean id="defaultDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

        <property name ="driverClassName" value ="${mysql.jdbc.driverClassName}" />

        <property name ="url" value ="${mysql.jdbc.url}" />

        <property name ="username" value ="${mysql.jdbc.username}" />

        <property name ="password" value ="${mysql.jdbc.password}" />

        <property name ="initialSize" value ="10"/>

        <property name ="maxActive" value ="30"/>

        <property name ="maxIdle" value ="15" />

        <property name ="minIdle" value ="15" />

        <property name ="testOnBorrow" value ="false" />

        <property name ="validationQuery" value ="select 1" />

        <property name ="timeBetweenEvictionRunsMillis" value ="10000" />

        <property name ="testWhileIdle" value ="true" /> 

        <property name ="numTestsPerEvictionRun" value ="3" />       

        <property name ="minEvictableIdleTimeMillis" value ="-1" />

    </bean>

    <!-- define the SqlSessionFactory -->

    <bean id="defaultSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

        <property name ="dataSource" ref="defaultDataSource" />

        <property name ="typeAliasesPackage" value ="com.sample" />

        <property name ="configLocation" value ="classpath:config/mybatisConfig.xml"/>

        <property name ="mapperLocations" value ="classpath*:com/sample/**/sql/*.xml" />

    </bean>

   

    <bean id="defaultSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">

        <constructor-arg ref="defaultSqlSessionFactory"/>

    </bean>

    <!-- transaction manager, use JtaTransactionManager for global tx -->

    <bean id="defaultTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

        <property name ="dataSource" ref="defaultDataSource" />

    </bean>

    <tx:annotation-driven transaction-manager="defaultTransactionManager"/>

SampleServiceResourceImpl.java

@RequestMapping("/category")

@Controller

public class SampleServiceResourceImpl extends BaseServiceResource implements SampleServiceResource {


    @Autowired

    private SampleBiz sampleBiz;

    @RequestMapping(value = "/samples/tx01", method = RequestMethod.POST)

    public ModelAndView txSample01(@RequestBody SampleVO sampleVO) throws Exception {


        log.debug("tx sample value : " + sampleVO.toString());

        int result = sampleBiz.txSample01(sampleVO);


        return success("list", sampleBiz.getSampleList());

    }

 }

SampleDAOImpl.java


@Service("sampleBiz")

public class SampleBizImpl extends BaseBIZ implements SampleBiz{


    @Autowired

    private SampleDAO sampleDAO;

    @Override

    @Transactional(readOnly = false)

    public int txSample01(SampleVO sampleVO) throws Exception {

        return 

sampleDAO.insertSample(sampleVO);

    }

}
 

SampleBizImpl.java

@Repository("sampleDAO")

public class SampleDAOImpl extends BaseDAO implements SampleDAO {


    

@Override

    public int insertSample(SampleVO sampleVO) throws Exception {           

        return getSqlSession().insert("com.sample.guide.dao.SampleDAO.insertSample", sampleVO);

    }

}



728x90