ssm小小整合
ssm整合:
首先是建立資料庫:
CREATE TABLE `user` ( `id` int(20) NOT NULL, `username` varchar(255) NOT NULL, `password` varchar(255) NOT NULL ) ; ALTER TABLE `user` ADD PRIMARY KEY (`id`); ALTER TABLE `user` MODIFY `id` int(20) NOT NULL AUTO_INCREMENT;
插入一條測試資料:
在me中建立一個新的web工程
springmvc的基本jar
mybatis基本jar
mybatis-spring整合jar
工程目錄:
MyEclipse可以自動匯入jar
mybatis的jar還需要自己手動匯入到lib檔案中
有些配置檔案是系統自動生成
無須追求一致
lib資料夾
基於mappe開發
1.spring和mybatis的整合:
SqlMapConfig.xml
此時可以根據自己的需要去設定<setting>......
根據自己開發的需求
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 載入 對映檔案 --> <mappers> <package name="com.test.mapper"/> </mappers> </configuration>
mapper和java檔案需要在同一個目錄(mybatis知識)
applicationContext.xml
<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> <!-- 載入配置檔案 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 資料來源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <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="maxActive" value="10" /> <property name="maxIdle" value="5" /> </bean> <!-- 配置sqlSessinFactory --> <!-- org.mybatis.spring.SqlSessionFactoryBean --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 載入mybatis的配置檔案 --> <property name="configLocation" value="mybatis/SqlMapConfig.xml" /> <!-- 資料來源 --> <property name="dataSource" ref="dataSource" /> </bean> <!-- mapper配置 MapperFactoryBean:根據mapper介面生成代理物件 --> <!-- mapperInterface指定mapper介面 --> <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.test.mapper.UserMapper"/> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> </beans>
此時的整合基本結束
整合測試:
UserMapper.java
public interface UserMapper { //根據id查詢使用者資訊 public User findUserById(int id) throws Exception; public List<User> findUserAll() throws Exception; }
UserMapper.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="com.test.mapper.UserMapper"> <select id="findUserById" parameterType="int" resultType="com.test.pojo.User"> SELECT * FROM USER WHERE id=#{value} </select> <select id="findUserAll" parameterType="int" resultType="com.test.pojo.User"> SELECT * FROM USER </select> </mapper>
測試:
public class test { public static void main(String[] args) throws Exception { ApplicationContext app = new ClassPathXmlApplicationContext("spring/applicationContext.xml"); UserMapper user = (UserMapper) app.getBean("userMapper"); User u = user.findUserById(1); System.out.println(u); } }
DEBUG [main] - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6b81ce95] DEBUG [main] - Returning JDBC Connection to DataSource User [id=1, username=user, password=1234]
此時資料是可以成功從資料庫中查詢!
2.關於web網頁實現測試
配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <!-- 配置DispatcherServlet --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
基本的配置也就是 這些了,其他的根據自己專案的需求去設定。
配置springmvc.xml
<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> <!-- 配置自動掃描的包 --> <context:component-scan base-package="com.test"></context:component-scan> <!--註解開發驅動--> <mvc:annotation-driven></mvc:annotation-driven> <!-- 配置檢視解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
配置handler
SelectUser.java
@Controller public class SelectUser { @RequestMapping("/list") public String selectList(Model model) throws Exception{return "list"; } }
此時先進行程式碼的測試
index.jsp
<body> <a href="<%=request.getContextPath() %>/list">SelectAll</a> </body>
list.jsp
<body> hello </body>
然後把專案放在tomcat上執行
點選超連結進行跳轉
此時可以成功跳轉
<%=request.getContextPath() %>加上,否則跳轉的路徑可能會出問題
在進行資料的查詢測試:
UserMapperImp.java
把資料庫的查詢以及相關返回均放在一個類中!
public class UserMapperImp { static ApplicationContext app = new ClassPathXmlApplicationContext("spring/applicationContext.xml"); //查詢全部 public static List<User> getUsers() throws Exception{ UserMapper um = (UserMapper) app.getBean("userMapper"); List<User> u =um.findUserAll(); return u; } }
SelectUser.java
@Controller public class SelectUser { @RequestMapping("/list") public String selectList(Model model) throws Exception{ model.addAttribute("users", UserMapperImp.getUsers()); return "list"; } }
list.jsp
對資料庫進行迴圈遍歷的獲取!
<table border="1" cellpadding="0" cellspacing="0" width="500"> <tr> <td>id</td> <td>user</td> <td>pwd</td> </tr> <c:forEach items="${users}" var="u"> <tr> <td>${u.id}</td> <td>${u.username}</td> <td>${u.password}</td> </tr> </c:forEach> </table>
此時再次重新啟動tomcat
點選超連結