spring - mybatis 세팅
가장먼저 라이브러리 업데이트를 해야된다.
maven 요즘 다들 쓰져?
pom.xml에 해당내용을 추가한다.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
<dependency>
<groupId>com.kenai.nbpwr</groupId>
<artifactId>org-apache-commons-dbcp</artifactId>
<version>1.2.2-201002241055</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.0</version>
</dependency>
저장하면 자동으로 라이브러리가 업데이트된다.
그후에 mybatis 환경파일을 만든다.
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.pgmman.webmvc"/>
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<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="typeAliasesPackage" value="com.pgmman.webmvc.vo"/>
<property name="mapperLocations" value="classpath*:com/pgmman/webmvc/mapper/*.xml"/>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
중간에 환경파일이 하나 있을것이다 jdbc.properties 요놈을 생성해준다.
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/gmProject
jdbc.username=비밀
jdbc.password=비밀
마지막으로 web.xml 에 해당 환경을 읽도록 추가해준다.
전부 다읽도록 수정했다.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/*-context.xml
</param-value>
</context-param>
테스트해야지.
테스트할 쿼리를 user.xml에 생성했다.
<?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="user">
<select id="getUserList" resultType="UserInfoVO">
SELECT user_id,user_name
FROM user
</select>
</mapper>
dao도 생성한다.
@Repository
public class UserInfoDao {
@Autowired
private SqlSession sqlSession;
public List<UserInfoVO> getUserList() {
return sqlSession.selectList("user.getUserList");
}
}
service생성한다.
@Service
public class UserInfoService {
@Autowired
private UserInfoDao userInfoDao;
public List<UserInfoVO> getUserList() {
return userInfoDao.getUserList();
}
}
Controller생성한다.
@Controller
public class BoardController {
@Autowired
private UserInfoService userInfoService;
private static final Logger logger = LoggerFactory.getLogger(BoardController.class);
@RequestMapping(value = "/board.gm", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
logger.info("Welcome board!! The client locale is {}.", locale);
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate );
model.addAttribute("userList", userInfoService.getUserList());
return "board";
}
}
화면 생성한다.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" pageEncoding="utf-8" %>
<%
//UserInfoVO user;
//user = request.getAttribute("userList");
%>
<html>
<head>
<title>Board</title>
</head>
<body>
<h1>
Hello world!
</h1>
<P> The time on the server is ${serverTime}. </P>
<c:forEach var='user' items='${userList}'>
${user.user_id}/${user.user_name}<br>
</c:forEach>
</body>
</html>
자 그럼 보일것이다.
끝.
'Dev > linux' 카테고리의 다른 글
라즈베리파이 리눅스에 mysql 설치 백업 복원 (0) | 2014.01.22 |
---|---|
라즈베리파이 리눅스 아파치 톰캣 설치 (0) | 2014.01.21 |
라즈베리파이 리눅스 ftp서버 설치 (0) | 2014.01.21 |
라즈베리파이 리눅스 파티션 조절 (0) | 2014.01.21 |
라즈베리파이 라즈비안 xbmc (6) | 2013.07.16 |