- 建立web项目
- 导入jar包(spring.jar, spring-webmvc.jar, commons-logging.jar。其他jar包为hibernate相关jar包)
3 . 修改web.xml,文件内容如下
<code class="hljs xml has-numbering"><span class="hljs-pi"><?xml version="1.0" encoding="UTF-8"?></span> <span class="hljs-tag"><<span class="hljs-title">web-app</span> <span class="hljs-attribute">version</span>=<span class="hljs-value">"2.5"</span> <span class="hljs-attribute">xmlns</span>=<span class="hljs-value">"http://java.sun.com/xml/ns/javaee"</span> <span class="hljs-attribute">xmlns:xsi</span>=<span class="hljs-value">"http://www.w3.org/2001/XMLSchema-instance"</span> <span class="hljs-attribute">xsi:schemaLocation</span>=<span class="hljs-value">"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"</span>></span> <span class="hljs-tag"><<span class="hljs-title">servlet</span>></span> <span class="hljs-tag"><<span class="hljs-title">servlet-name</span>></span>springmvc<span class="hljs-tag"></<span class="hljs-title">servlet-name</span>></span> <span class="hljs-tag"><<span class="hljs-title">servlet-class</span>></span> org.springframework.web.servlet.DispatcherServlet <span class="hljs-tag"></<span class="hljs-title">servlet-class</span>></span> <span class="hljs-tag"><<span class="hljs-title">init-param</span>></span> <span class="hljs-tag"><<span class="hljs-title">param-name</span>></span>contextConfigLocation<span class="hljs-tag"></<span class="hljs-title">param-name</span>></span> <span class="hljs-tag"><<span class="hljs-title">param-value</span>></span>/WEB-INF/hib-config.xml,/WEB-INF/springmvc-servlet.xml<span class="hljs-tag"></<span class="hljs-title">param-value</span>></span> <span class="hljs-tag"></<span class="hljs-title">init-param</span>></span> <span class="hljs-tag"><<span class="hljs-title">load-on-startup</span>></span>1<span class="hljs-tag"></<span class="hljs-title">load-on-startup</span>></span> <span class="hljs-tag"></<span class="hljs-title">servlet</span>></span> <span class="hljs-tag"><<span class="hljs-title">servlet-mapping</span>></span> <span class="hljs-tag"><<span class="hljs-title">servlet-name</span>></span>springmvc<span class="hljs-tag"></<span class="hljs-title">servlet-name</span>></span> <span class="hljs-tag"><<span class="hljs-title">url-pattern</span>></span>*.do<span class="hljs-tag"></<span class="hljs-title">url-pattern</span>></span> <span class="hljs-tag"></<span class="hljs-title">servlet-mapping</span>></span> <span class="hljs-tag"></<span class="hljs-title">web-app</span>></span> </code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li></ul>
4.springmvc-servlet.xml配置内容如下:
<code class="hljs xml has-numbering"><span class="hljs-pi"><?xml version="1.0" encoding="UTF-8"?></span> <span class="hljs-tag"><<span class="hljs-title">beans</span> <span class="hljs-attribute">xmlns</span>=<span class="hljs-value">"http://www.springframework.org/schema/beans"</span> <span class="hljs-attribute">xmlns:xsi</span>=<span class="hljs-value">"http://www.w3.org/2001/XMLSchema-instance"</span> <span class="hljs-attribute">xmlns:p</span>=<span class="hljs-value">"http://www.springframework.org/schema/p"</span> <span class="hljs-attribute">xmlns:context</span>=<span class="hljs-value">"http://www.springframework.org/schema/context"</span> <span class="hljs-attribute">xsi:schemaLocation</span>=<span class="hljs-value">"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"</span>></span> <span class="hljs-comment"><!-- 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 --></span> <span class="hljs-tag"><<span class="hljs-title">context:component-scan</span> <span class="hljs-attribute">base-package</span>=<span class="hljs-value">"com.sxt"</span>/></span> <span class="hljs-comment"><!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --></span> <span class="hljs-tag"><<span class="hljs-title">bean</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"</span>/></span> <span class="hljs-comment"><!--对模型视图名称的解析,即在模型视图名称添加前后缀 --></span> <span class="hljs-tag"><<span class="hljs-title">bean</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"org.springframework.web.servlet.view.InternalResourceViewResolver"</span> <span class="hljs-attribute">p:suffix</span>=<span class="hljs-value">".jsp"</span>/></span> <span class="hljs-tag"></<span class="hljs-title">beans</span>></span> </code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li></ul>
5 . hib-config.xml(配置了spring集成hibernate)
<code class="hljs xml has-numbering"><span class="hljs-pi"><?xml version="1.0" encoding="UTF-8"?></span> <span class="hljs-tag"><<span class="hljs-title">beans</span> <span class="hljs-attribute">xmlns</span>=<span class="hljs-value">"http://www.springframework.org/schema/beans"</span> <span class="hljs-attribute">xmlns:xsi</span>=<span class="hljs-value">"http://www.w3.org/2001/XMLSchema-instance"</span> <span class="hljs-attribute">xmlns:aop</span>=<span class="hljs-value">"http://www.springframework.org/schema/aop"</span> <span class="hljs-attribute">xmlns:tx</span>=<span class="hljs-value">"http://www.springframework.org/schema/tx"</span> <span class="hljs-attribute">xmlns:context</span>=<span class="hljs-value">"http://www.springframework.org/schema/context"</span> <span class="hljs-attribute">xsi:schemaLocation</span>=<span class="hljs-value">" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd "</span>></span> <span class="hljs-tag"><<span class="hljs-title">context:component-scan</span> <span class="hljs-attribute">base-package</span>=<span class="hljs-value">"com.sxt"</span>/></span> <span class="hljs-comment"><!-- 支持aop注解 --></span> <span class="hljs-tag"><<span class="hljs-title">aop:aspectj-autoproxy</span> /></span> <span class="hljs-tag"><<span class="hljs-title">bean</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"dataSource"</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"org.apache.commons.dbcp.BasicDataSource"</span>></span> <span class="hljs-tag"><<span class="hljs-title">property</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"driverClassName"</span> <span class="hljs-attribute">value</span>=<span class="hljs-value">"com.mysql.jdbc.Driver"</span>></span> <span class="hljs-tag"></<span class="hljs-title">property</span>></span> <span class="hljs-tag"><<span class="hljs-title">property</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"url"</span> <span class="hljs-attribute">value</span>=<span class="hljs-value">"jdbc:mysql://localhost:3306/myhib"</span>></span><span class="hljs-tag"></<span class="hljs-title">property</span>></span> <span class="hljs-tag"><<span class="hljs-title">property</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"username"</span> <span class="hljs-attribute">value</span>=<span class="hljs-value">"root"</span>></span><span class="hljs-tag"></<span class="hljs-title">property</span>></span> <span class="hljs-tag"><<span class="hljs-title">property</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"password"</span> <span class="hljs-attribute">value</span>=<span class="hljs-value">"123456"</span>></span><span class="hljs-tag"></<span class="hljs-title">property</span>></span> <span class="hljs-tag"></<span class="hljs-title">bean</span>></span> <span class="hljs-tag"><<span class="hljs-title">bean</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"sessionFactory"</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"</span>></span> <span class="hljs-tag"><<span class="hljs-title">property</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"dataSource"</span>></span> <span class="hljs-tag"><<span class="hljs-title">ref</span> <span class="hljs-attribute">bean</span>=<span class="hljs-value">"dataSource"</span> /></span> <span class="hljs-tag"></<span class="hljs-title">property</span>></span> <span class="hljs-tag"><<span class="hljs-title">property</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"hibernateProperties"</span>></span> <span class="hljs-tag"><<span class="hljs-title">props</span>></span> <span class="hljs-comment"><!-- key的名字前面都要加hibernate. --></span> <span class="hljs-tag"><<span class="hljs-title">prop</span> <span class="hljs-attribute">key</span>=<span class="hljs-value">"hibernate.dialect"</span>></span> org.hibernate.dialect.MySQLDialect <span class="hljs-tag"></<span class="hljs-title">prop</span>></span> <span class="hljs-tag"><<span class="hljs-title">prop</span> <span class="hljs-attribute">key</span>=<span class="hljs-value">"hibernate.show_sql"</span>></span>true<span class="hljs-tag"></<span class="hljs-title">prop</span>></span> <span class="hljs-tag"><<span class="hljs-title">prop</span> <span class="hljs-attribute">key</span>=<span class="hljs-value">"hibernate.hbm2ddl.auto"</span>></span>update<span class="hljs-tag"></<span class="hljs-title">prop</span>></span> <span class="hljs-tag"></<span class="hljs-title">props</span>></span> <span class="hljs-tag"></<span class="hljs-title">property</span>></span> <span class="hljs-tag"><<span class="hljs-title">property</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"packagesToScan"</span>></span> <span class="hljs-tag"><<span class="hljs-title">value</span>></span>com.sxt.po<span class="hljs-tag"></<span class="hljs-title">value</span>></span> <span class="hljs-tag"></<span class="hljs-title">property</span>></span> <span class="hljs-tag"></<span class="hljs-title">bean</span>></span> <span class="hljs-tag"><<span class="hljs-title">bean</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"hibernateTemplate"</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"org.springframework.orm.hibernate3.HibernateTemplate"</span> ></span> <span class="hljs-tag"><<span class="hljs-title">property</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"sessionFactory"</span> <span class="hljs-attribute">ref</span>=<span class="hljs-value">"sessionFactory"</span>></span><span class="hljs-tag"></<span class="hljs-title">property</span>></span> <span class="hljs-tag"></<span class="hljs-title">bean</span>></span> <span class="hljs-comment"><!--配置一个JdbcTemplate实例--></span> <span class="hljs-tag"><<span class="hljs-title">bean</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"jdbcTemplate"</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"org.springframework.jdbc.core.JdbcTemplate"</span>></span> <span class="hljs-tag"><<span class="hljs-title">property</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"dataSource"</span> <span class="hljs-attribute">ref</span>=<span class="hljs-value">"dataSource"</span>/></span> <span class="hljs-tag"></<span class="hljs-title">bean</span>></span> <span class="hljs-comment"><!-- 配置事务管理 --></span> <span class="hljs-tag"><<span class="hljs-title">bean</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"txManager"</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"org.springframework.orm.hibernate3.HibernateTransactionManager"</span> ></span> <span class="hljs-tag"><<span class="hljs-title">property</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"sessionFactory"</span> <span class="hljs-attribute">ref</span>=<span class="hljs-value">"sessionFactory"</span>></span><span class="hljs-tag"></<span class="hljs-title">property</span>></span> <span class="hljs-tag"></<span class="hljs-title">bean</span>></span> <span class="hljs-tag"><<span class="hljs-title">tx:annotation-driven</span> <span class="hljs-attribute">transaction-manager</span>=<span class="hljs-value">"txManager"</span> /></span> <span class="hljs-tag"><<span class="hljs-title">aop:config</span>></span> <span class="hljs-tag"><<span class="hljs-title">aop:pointcut</span> <span class="hljs-attribute">expression</span>=<span class="hljs-value">"execution(public * com.sxt.service.impl.*.*(..))"</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"businessService"</span>/></span> <span class="hljs-tag"><<span class="hljs-title">aop:advisor</span> <span class="hljs-attribute">advice-ref</span>=<span class="hljs-value">"txAdvice"</span> <span class="hljs-attribute">pointcut-ref</span>=<span class="hljs-value">"businessService"</span> /></span> <span class="hljs-tag"></<span class="hljs-title">aop:config</span>></span> <span class="hljs-tag"><<span class="hljs-title">tx:advice</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"txAdvice"</span> <span class="hljs-attribute">transaction-manager</span>=<span class="hljs-value">"txManager"</span> ></span> <span class="hljs-tag"><<span class="hljs-title">tx:attributes</span>></span> <span class="hljs-tag"><<span class="hljs-title">tx:method</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"find*"</span> <span class="hljs-attribute">read-only</span>=<span class="hljs-value">"true"</span> <span class="hljs-attribute">propagation</span>=<span class="hljs-value">"NOT_SUPPORTED"</span> /></span> <span class="hljs-comment"><!-- get开头的方法不需要在事务中运行 。 有些情况是没有必要使用事务的,比如获取数据。开启事务本身对性能是有一定的影响的--></span> <span class="hljs-tag"><<span class="hljs-title">tx:method</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"*"</span>/></span> <span class="hljs-comment"><!-- 其他方法在实务中运行 --></span> <span class="hljs-tag"></<span class="hljs-title">tx:attributes</span>></span> <span class="hljs-tag"></<span class="hljs-title">tx:advice</span>></span> <span class="hljs-tag"></<span class="hljs-title">beans</span>></span> </code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li><li>29</li><li>30</li><li>31</li><li>32</li><li>33</li><li>34</li><li>35</li><li>36</li><li>37</li><li>38</li><li>39</li><li>40</li><li>41</li><li>42</li><li>43</li><li>44</li><li>45</li><li>46</li><li>47</li><li>48</li><li>49</li><li>50</li><li>51</li><li>52</li><li>53</li><li>54</li><li>55</li><li>56</li><li>57</li><li>58</li><li>59</li><li>60</li><li>61</li><li>62</li><li>63</li><li>64</li><li>65</li><li>66</li><li>67</li><li>68</li><li>69</li><li>70</li><li>71</li><li>72</li><li>73</li><li>74</li><li>75</li><li>76</li><li>77</li><li>78</li><li>79</li><li>80</li><li>81</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li><li>29</li><li>30</li><li>31</li><li>32</li><li>33</li><li>34</li><li>35</li><li>36</li><li>37</li><li>38</li><li>39</li><li>40</li><li>41</li><li>42</li><li>43</li><li>44</li><li>45</li><li>46</li><li>47</li><li>48</li><li>49</li><li>50</li><li>51</li><li>52</li><li>53</li><li>54</li><li>55</li><li>56</li><li>57</li><li>58</li><li>59</li><li>60</li><li>61</li><li>62</li><li>63</li><li>64</li><li>65</li><li>66</li><li>67</li><li>68</li><li>69</li><li>70</li><li>71</li><li>72</li><li>73</li><li>74</li><li>75</li><li>76</li><li>77</li><li>78</li><li>79</li><li>80</li><li>81</li></ul>
6 . WEB-INF下建立jsp文件夹,并且将index.jsp放入该文件夹下。Index.jsp的内容如下
<code class="hljs xml has-numbering"><span class="vbscript"><%@ page language=<span class="hljs-string">"java"</span> import=<span class="hljs-string">"java.util.*"</span> pageEncoding=<span class="hljs-string">"gbk"</span>%></span> <span class="vbscript"><% <span class="hljs-built_in">String</span> path = <span class="hljs-built_in">request</span>.getContextPath(); <span class="hljs-built_in">String</span> basePath = <span class="hljs-built_in">request</span>.getScheme()+<span class="hljs-string">"://"</span>+<span class="hljs-built_in">request</span>.getServerName()+<span class="hljs-string">":"</span>+<span class="hljs-built_in">request</span>.getServerPort()+path+<span class="hljs-string">"/"</span>; %></span> <span class="hljs-doctype"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"></span> <span class="hljs-tag"><<span class="hljs-title">html</span>></span> <span class="hljs-tag"><<span class="hljs-title">head</span>></span> <span class="hljs-tag"><<span class="hljs-title">base</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">"<%=basePath%>"</span>></span> <span class="hljs-tag"><<span class="hljs-title">title</span>></span>My JSP 'index.jsp' starting page<span class="hljs-tag"></<span class="hljs-title">title</span>></span> <span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"pragma"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"no-cache"</span>></span> <span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"cache-control"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"no-cache"</span>></span> <span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"expires"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"0"</span>></span> <span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"keywords"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"keyword1,keyword2,keyword3"</span>></span> <span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"description"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"This is my page"</span>></span> <span class="hljs-comment"><!-- <link rel="stylesheet" type="text/css" href="styles.css"> --></span> <span class="hljs-tag"></<span class="hljs-title">head</span>></span> <span class="hljs-tag"><<span class="hljs-title">body</span>></span> <span class="hljs-tag"><<span class="hljs-title">h1</span>></span>**********${params.uname}<span class="hljs-tag"></<span class="hljs-title">h1</span>></span> <span class="hljs-tag"><<span class="hljs-title">h1</span>></span>**********${requestScope.u}<span class="hljs-tag"></<span class="hljs-title">h1</span>></span> <span class="hljs-tag"><<span class="hljs-title">h1</span>></span>**********${requestScope.user}<span class="hljs-tag"></<span class="hljs-title">h1</span>></span> <span class="hljs-tag"></<span class="hljs-title">body</span>></span> <span class="hljs-tag"></<span class="hljs-title">html</span>></span> </code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li><li>29</li><li>30</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li><li>29</li><li>30</li></ul>
7 .建立整个项目的包结构和相关类。如下图所示
8 . User、UserDao、UserService、UserController类的代码如下
<code class="hljs java has-numbering"><span class="hljs-keyword">package</span> com.sxt.po; <span class="hljs-keyword">import</span> javax.persistence.Entity; <span class="hljs-keyword">import</span> javax.persistence.GeneratedValue; <span class="hljs-keyword">import</span> javax.persistence.GenerationType; <span class="hljs-keyword">import</span> javax.persistence.Id; <span class="hljs-annotation">@Entity</span> <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> {</span> <span class="hljs-annotation">@Id</span> <span class="hljs-annotation">@GeneratedValue</span>(strategy=GenerationType.AUTO) <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> id; <span class="hljs-keyword">private</span> String uname; <span class="hljs-keyword">private</span> String pwd; <span class="hljs-keyword">public</span> String <span class="hljs-title">getPwd</span>() { <span class="hljs-keyword">return</span> pwd; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setPwd</span>(String pwd) { <span class="hljs-keyword">this</span>.pwd = pwd; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">getId</span>() { <span class="hljs-keyword">return</span> id; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setId</span>(<span class="hljs-keyword">int</span> id) { <span class="hljs-keyword">this</span>.id = id; } <span class="hljs-keyword">public</span> String <span class="hljs-title">getUname</span>() { <span class="hljs-keyword">return</span> uname; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setUname</span>(String uname) { <span class="hljs-keyword">this</span>.uname = uname; } } <span class="hljs-keyword">package</span> com.sxt.dao; <span class="hljs-keyword">import</span> javax.annotation.Resource; <span class="hljs-keyword">import</span> org.springframework.orm.hibernate3.HibernateTemplate; <span class="hljs-keyword">import</span> org.springframework.stereotype.Repository; <span class="hljs-keyword">import</span> com.sxt.po.User; <span class="hljs-annotation">@Repository</span>(<span class="hljs-string">"userDao"</span>) <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserDao</span> {</span> <span class="hljs-annotation">@Resource</span> <span class="hljs-keyword">private</span> HibernateTemplate hibernateTemplate; <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">add</span>(User u){ System.out.println(<span class="hljs-string">"UserDao.add()"</span>); hibernateTemplate.save(u); } <span class="hljs-keyword">public</span> HibernateTemplate <span class="hljs-title">getHibernateTemplate</span>() { <span class="hljs-keyword">return</span> hibernateTemplate; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setHibernateTemplate</span>(HibernateTemplate hibernateTemplate) { <span class="hljs-keyword">this</span>.hibernateTemplate = hibernateTemplate; } } <span class="hljs-keyword">package</span> com.sxt.service; <span class="hljs-keyword">import</span> javax.annotation.Resource; <span class="hljs-keyword">import</span> org.springframework.stereotype.Service; <span class="hljs-keyword">import</span> com.sxt.dao.UserDao; <span class="hljs-keyword">import</span> com.sxt.po.User; <span class="hljs-annotation">@Service</span>(<span class="hljs-string">"userService"</span>) <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserService</span> {</span> <span class="hljs-annotation">@Resource</span> <span class="hljs-keyword">private</span> UserDao userDao; <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">add</span>(String uname){ System.out.println(<span class="hljs-string">"UserService.add()"</span>); User u = <span class="hljs-keyword">new</span> User(); u.setUname(uname); userDao.add(u); } <span class="hljs-keyword">public</span> UserDao <span class="hljs-title">getUserDao</span>() { <span class="hljs-keyword">return</span> userDao; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setUserDao</span>(UserDao userDao) { <span class="hljs-keyword">this</span>.userDao = userDao; } } <span class="hljs-keyword">package</span> com.sxt.web; <span class="hljs-keyword">import</span> javax.annotation.Resource; <span class="hljs-keyword">import</span> org.springframework.stereotype.Controller; <span class="hljs-keyword">import</span> org.springframework.ui.ModelMap; <span class="hljs-keyword">import</span> org.springframework.web.bind.annotation.RequestMapping; <span class="hljs-keyword">import</span> org.springframework.web.bind.annotation.RequestParam; <span class="hljs-keyword">import</span> org.springframework.web.bind.annotation.SessionAttributes; <span class="hljs-keyword">import</span> com.sxt.po.User; <span class="hljs-keyword">import</span> com.sxt.service.UserService; <span class="hljs-annotation">@Controller</span>(<span class="hljs-string">"userController"</span>) <span class="hljs-annotation">@RequestMapping</span>(<span class="hljs-string">"/user.do"</span>) <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserController</span> {</span> <span class="hljs-annotation">@Resource</span> <span class="hljs-keyword">private</span> UserService userService; <span class="hljs-annotation">@RequestMapping</span>(params=<span class="hljs-string">"method=reg"</span>) <span class="hljs-keyword">public</span> String <span class="hljs-title">reg</span>(String uname) { System.out.println(<span class="hljs-string">"HelloController.handleRequest()"</span>); userService.add(uname); <span class="hljs-keyword">return</span> <span class="hljs-string">"index"</span>; } <span class="hljs-keyword">public</span> UserService <span class="hljs-title">getUserService</span>() { <span class="hljs-keyword">return</span> userService; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setUserService</span>(UserService userService) { <span class="hljs-keyword">this</span>.userService = userService; } }</code>