maven + spring + mybatis 설정

2013. 11. 15. 13:39개발관련기록/Spring

반응형

참고 URL : https://gist.github.com/changhwa





기본적은 Spring+Maven은 STS의 Spring MVC Template를 이용해 구성했다.

인터넷 예제가 동작안하는것 들도 있기에 따로 정리해보았다.

pom.xml에서 추가된 부분

123456789101112131415161718192021222324252627
<!-- spring framework jdbc 설정 -->
<dependency>
<groupId> org.springframework</groupId >
<artifactId> spring-jdbc</artifactId >
<version> ${org.springframework-version}</version >
</dependency>
 
<!-- mysql -->
<dependency>
<groupId> mysql</groupId >
<artifactId> mysql-connector-java </artifactId>
<version> 5.1.18</ version>
</dependency>
 
<!-- mybatis -->
<dependency>
<groupId> org.mybatis</groupId >
<artifactId> mybatis</artifactId >
<version> 3.0.6</ version>
</dependency>
 
<!-- mybatis spring -->
<dependency>
<groupId> org.mybatis</groupId >
<artifactId> mybatis-spring </artifactId>
<version> 1.0.2</ version>
</dependency>
view rawpom.xml hosted with ❤ by GitHub

web.xml에서 수정된 부분

1234567
<context-param>
<param-name> contextConfigLocation</param-name >
<param-value>
/WEB-INF/spring/root-context.xml
classpath:mybatis/context-mybatis.xml
</param-value>
</context-param>
view rawweb.xml hosted with ❤ by GitHub


context-mybatis.xml 추가

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
 
<?xml version= "1.0" encoding ="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
 
<!-- Message Source -->
<bean id="rootMessageSource"
class= "org.springframework.context.support.ReloadableResourceBundleMessageSource" >
<property name= "basenames">
<list>
<value>classpath:messages/core</value >
</list>
</property>
<property name="fileEncodings" value="UTF-8" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
 
<bean
class= "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
<property name= "locations">
<value>classpath:properties/mysql.properties</value >
</property>
</bean>
 
<bean id="dataSource"
class= "org.springframework.jdbc.datasource.SimpleDriverDataSource" >
<property name= "driverClass" value="${jdbc.driverClass}" />
<property name= "url" value ="${jdbc.url}" />
<property name= "username" value="${jdbc.username}" />
<property name= "password" value="${jdbc.password}" />
</bean>
 
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
<property name= "dataSource" ref="dataSource" />
<property name= "mapperLocations" value="classpath:mybatis/mapper/**/*.xml" />
</bean>
 
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" >
<constructor-arg index= "0" ref="sqlSessionFactory" />
</bean>
 
 
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
<property name= "basePackage" value="com.narratage.reserve" />
</bean>
 
 
<bean id="transactionManager"
class= "org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name= "dataSource" ref="dataSource" />
</bean>
 
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
view rawcontext-mybatis.xml hosted with ❤ by GitHub


mysql.properties 파일 추가

1234
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/toby
jdbc.username=root
jdbc.password=1234
view rawmysql.properties hosted with ❤ by GitHub


user.xml (Query 추가)

123456789101112131415
 
<?xml version= "1.0" encoding ="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
 
<mapper namespace="com.narratage.reserve.user.dao.UserDao" > <!-- namespace에서 interface로 정의된 DAO를 정확히 명시해주어야 한다 -->
 
<cache />
 
<select id="userCount" resultType="int" >
SELECT count(*)
FROM users
</select >
 
</mapper>
view rawuser.xml hosted with ❤ by GitHub


Test소스

HomeController.java

12345678910111213141516171819202122232425
@Controller
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController. class);
 
@Autowired
private UserDao userDao;
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
logger.info("Welcome home! the client locale is "+ locale.toString());
Date date = new Date();
DateFormat dateFormat = DateFormat. getDateTimeInstance(DateFormat .LONG , DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
System. out.println("aaaa: "+userDao.userCount());
model.addAttribute( "serverTime", formattedDate );
return "home" ;
}
}
view rawHomeController.java hosted with ❤ by GitHub


"

반응형