一、问题的提出

        最近一直在看各种主流javaweb框架,感触颇深,一个好的框架可以让你的程序代码变得相当的明了,也便于后期的维护!目前用到的主流框架包括struts2+spring+mybatis和spring+springmvc+mybatis(注:个人觉得mybatis框架很好用,所以框架基本都会选择用mybatis)。其实很多的小伙伴都想自己配置个框架,但是基于怕报错啊、引入jar包、程序调试啊等等问题都放弃了!基于这个现象,本人在这里就详细的介绍下本人配置框架的详细过程,希望对读者有所帮助。

二、引入jar包

      这个问题一直是想搭建框架的小伙伴们的最大的一个障碍!很多人就是由于不知道导入什么样的jar而犯愁!对于这个问题我个人的想法是,jar包都是向下兼容的所以你大可不用担心你的jar包版本不对的问题,尽可能下载版本靠后的jar包就能解决这个问题。其次是不知道要导入多少个jar包,这确实是个棘手的问题,对于这个问题我的解决方案是:首选是在网上查找,这方面的资料网上还是很全面的  什么jar都可以下载 ,其次是注意struts2 以及spring 等的官网,官网上也会有详细的介绍每个jar的功能。这里我就不在详细的描述了。下面是我这个框架搭建需要的jar包截图,如果有需要jar包的可私下联系我,大家共同学习。

s

三、配置框架

1、首先让大家看看我配置完后的框架图,再来讲述我配置的流程


    2、配置流程

       2.1、首先任何一个系统都需要有其对应的数据库,这里我用的是mysql数据库,首先在数据库里新建个数据库,注意数据库的字符集最好选为utf-8的这样能防止数据库中文字符乱码。由于我只是配置框架,所以我新建数据库名为wayneTest数据库里也只建了一张表,如下图:


     2.2、建好数据库后,我用了个工具Mybatis Generator自动生成model类,dao,mapper文件.这个工具的详细的用法网上也很多,如果读者不懂得话也可以私下联系我

    这里给你们个链接  Mybatis 主页 http://code.google.com/p/mybatis/ 上下载 Mybatis mybatis-generator-core 。具体用法可参考http://qiuguo0205.iteye.com/blog/819100

     2.3、把这些生成好的代码放到项目下,接下来就是配置文件的书写了,我分为以下几步:

        2.3.1、书写jdbc.properties配置文件,为什么首先书写这个配置文件呢?因为项目肯定是要连接数据库的,接下来的spring的配置文件也会用到这个配置文件

[html]  view plain  copy
  1. db.driver=com.mysql.jdbc.Driver  

[html]  view plain  copy
  1. db.url=jdbc:mysql://127.0.0.1:3306/wayneTest?<span style="color:#ff6666;">characterEncoding=UTF-8</span>  

[html]  view plain  copy
  1. db.username=root  

[html]  view plain  copy
  1. db.password=wayne  

其中?characterEncoding=UTF-8这段代码是为了确保编码的一致性(注意代码中的”?“一定不能少。这个原因相信大家都懂得,不懂私下找我)这一段代码相信大家都能看懂,就是配置数据库连接访问的路径和用户名及密码。

2.3.2、书写log4j.properties配置文件,为什么接下来书写这个文件呢?因为这个文件是由于打印控制台信息以及保存项目运行日志的配置文件,虽说与框架没什么关系,但是还是很需要配置的,应为他可以起到查看程序运行日志啊打印程序执行sql等功能,方便调试和维护。(个人强烈建议任何项目都配上这个日志文件)

[html]  view plain  copy
  1. # Create names for settings  
  2. log4j.rootCategory=INFO, fileOut,stdout  
  3. log4j.appender.fileOut=org.apache.log4j.RollingFileAppender  
  4. log4j.appender.fileOut.File=${wayneTest.root}/WEB-INF/logs/igp_error.log  
  5. log4j.appender.dataerr=org.apache.log4j.ConsoleAppender  
  6. log4j.appender.fileOut.MaxFileSize=2000KB  
  7. log4j.appender.fileOut.MaxBackupIndex=2  
  8. log4j.appender.fileOut.layout=org.apache.log4j.PatternLayout  
  9. log4j.appender.fileOut.layout.ConversionPattern=%-2d{yyyy-MM-dd HH:mm} [%t] %5p %c:%L - %m%n  
  10. log4j.appender.fileOut.Threshold=INFO  
  11. #log4j.logger.com.ibatis=DEBUG  
  12. #log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG  
  13. #log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG  
  14. #log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG  
  15. log4j.logger.java.sql.Connection=DEBUG  
  16. #log4j.logger.java.sql.Statement=DEBUG  
  17. #log4j.logger.java.sql.PreparedStatement=DEBUG  
  18. #log4j.logger.java.sql.ResultSet=DEBUG  
  19. log4j.appender.stdout=org.apache.log4j.ConsoleAppender  
  20. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  
  21. log4j.appender.stdout.layout.ConversionPattern=[%d] [%-5p] %c %n--%m--%n  
[html]  view plain  copy
  1. 这里是这个配置文件的详细配置,其中 <span style="font-family: Arial, Helvetica, sans-serif;">log4j.logger.java.sql.Connection=DEBUG 这一句是打印程序执行的sql语句的(个人认为很有必要配出来),关于这些配置文件的意义,我给大家推荐链接  </span><span style="font-family: Arial, Helvetica, sans-serif;">http://blog.csdn.net/weiluo12/article/details/8185662</span>  
[html]  view plain  copy
  1. 2.3.3、书写mybatis_config.xml配置文件,这个配置文件主要是用来设置的,比如是否启用缓存、数据库超时时间、字段映射、<a target=_blank href="https://www.baidu.com/s?wd=%E5%BB%B6%E8%BF%9F%E5%8A%A0%E8%BD%BD&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1dWuWmsnvf4PAcLujnLPvnd0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHDkn10sP1fkPWDsnHTkPjnLrf" target="_blank" class="baidu-highlight" rel="nofollow">延迟加载</a>、配置分页等等,很有用这些配置。但是对于新手来说作用不大,可以不进行任何配置。当然了解学习下还是很好的,<span style="font-family: Arial, Helvetica, sans-serif;">我给大家推荐链接http://blog.csdn.net/liuzhen917/article/details/37876727</span>  
[html]  view plain  copy
  1. <span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="html"><?xml version="1.0" encoding="UTF-8" ?>    
  2. <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">     
  3. <configuration>    
  4. <!--   
  5.     <typeAliases>    
  6.         <typeAlias alias="user" type="wayne.model.User"/>    
  7.     </typeAliases>    
  8.        
  9.     <mappers>    
  10.         <mapper resource="wayne/sqlmap/UserMapper.xml"/>    
  11.     </mappers>    
  12.       
  13.     -->  
  14. </configuration>  
[html]  view plain  copy
  1. 2.3.4、书写applicationContext.xml配置文件,这个文件很重要,相当重要,下面我给出这部分的代码,我会详细介绍这部分内容  
[html]  view plain  copy
  1. <pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:aop="http://www.springframework.org/schema/aop"  
  4.     xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  7.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
  8.     http://www.springframework.org/schema/aop   
  9.     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
  10.     http://www.springframework.org/schema/tx    
  11.     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">  
  12.        
  13.     <!-- 配置DataSource数据源 -->  
  14.     <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  15.         <property name="locations">  
  16.         <!-- 导入属性配置文件 -->  
  17.             <value>classpath:jdbc.properties</value>  
  18.         </property>  
  19.     </bean>  
  20.      <!--配置数据连接-->   
  21.     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">     
  22.     <!-- 指定连接数据库的JDBC驱动 -->  
  23.         <property name="driverClassName" value="${db.driver}" />  
  24.         <!-- 连接数据库所用的URL -->  
  25.         <property name="url" value="${db.url}" />  
  26.          <!-- 连接数据库的用户名 -->  
  27.         <property name="username" value="${db.username}" />  
  28.         <!-- 连接数据库的密码 -->  
  29.         <property name="password" value="${db.password}" />  
  30.     </bean>  
  31.        
  32.     <!-- 配置事务管理器,注意这里的dataSource和SqlSessionFactoryBean的dataSource要一致,不然事务就没有作用了 -->  
  33.     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  34.         <property name="dataSource" ref="dataSource" />  
  35.            
  36.     </bean>  
  37.            
  38.    <!-- 创建SqlSessionFactory,同时指定数据源 这里要注意Habernate 使用的 SessionFactory;Mybatis  的 SqlSessionFactory -->  
  39.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  40.         <property name="dataSource" ref="dataSource" />   
  41.          <!-- 创建SqlSessionFactory,同时指定数据源 -->  
  42.          <!-- 集成myBaits框架,配置sqlSessionFatory -->  
  43.         <property name="configLocation" value="classpath:mybatis_config.xml"></property>  
  44.          <!-- 引入sqlmap下的xml文件 -->  
  45.         <property name="mapperLocations" value="classpath:wayne/sqlmap/*.xml"></property>  
  46.     </bean>  
  47.       <!--把xml文件注入到mapperInterface -->  
  48.     <bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">    
  49.         <property name="mapperInterface" value="wayne.dao.UserMapper"/>    
  50.         <property name="sqlSessionFactory" ref="sqlSessionFactory" />    
  51.     </bean>    
  52.    <!--把dao注入到action -->  
  53.     <bean id="userAction" class="wayne.action.UserAction">    
  54.         <property name="userDao" ref="userDao"></property>    
  55.     </bean>    
  56.        
  57.     <!-- 单独配置一个Mapper; 这种模式就是得给每个mapper接口配置一个bean -->  
  58.     <!--   
  59.     <bean id="accountMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">   
  60.         <property name="mapperInterface" value="com.hoo.mapper.AccountMapper" />  
  61.         <property name="sqlSessionFactory" ref="sqlSessionFactory" />   
  62.     </bean>   
  63.     <bean id="companyMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">   
  64.         <property name="mapperInterface" value="com.hoo.mapper.CompanyMapper" />  
  65.         <property name="sqlSessionFactory" ref="sqlSessionFactory" />   
  66.     </bean>  
  67.      -->   
  68.     <!-- 通过扫描的模式,扫描目录在com/hoo/mapper目录下,所有的mapper都继承SqlMapper接口的接口, 这样一个bean就可以了 <br>      
  69.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  70.         <property name="basePackage" value="wayne.sqlmap"/>  
  71.         <property name="markerInterface" value="wayne.sqlmap.SqlMapper"/>  
  72.     </bean>  
  73.     -->  
  74.  </beans>  

在上面的这个配置文件里每一步我都详细的注明了它的用途,每一步都必不可少!如果还有没看懂的地方,私下找我共同探讨;到了这里的话spring已经整合完mybatis

[html]  view plain  copy
  1. 2.3.5、书写struts.xml配置文件,struts配置文件主要用于和前台交互,其本质是个过滤器,下面我给出这部分的代码  
[html]  view plain  copy
  1. <pre name="code" class="html"><?xml version="1.0" encoding="UTF-8" ?>    
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">    
  3. <struts>    
  4. <constant name="struts.i18n.encoding" value="UTF-8" />    
[html]  view plain  copy
  1. <pre name="code" class="html"> <!--   这里的namespace空间名,由于我只是做个简单的登陆页面所以用缺省“/”代替,如果项目比较庞大的话可以通过不同的命名空间来区分-->  
<package name="json" namespace="/" extends="struts-default"> <action name="login" method="login" class="wayne.action.UserAction"> <result name="success">main.jsp</result> <!-- 当页面表单填写不合法时,这里一定要加上下面这句 例如页面上input输入框有name=“id”的那种--> <!-- <result name="input">error.jsp</result> --> <result name="error">error.jsp</result> </action> </package> </struts>
[html]  view plain  copy
  1. <pre name="code" class="html">2.3.6、书写web.xml配置文件,这个文件很重要,下面我给出这部分的代码,我会详细介绍这部分内容  
[html]  view plain  copy
  1. <pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?>    
  2. <web-app version="2.4"     
  3.     xmlns="http://java.sun.com/xml/ns/j2ee"     
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee     
  6.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">    
  7.   <!-- web.xml 的加载顺序是:ServletContext-> context-param ->listener -> filter -> servlet,  
  8.                          而同个类型之间的实际程序调用的时候的顺序是根据对应的 mapping 的顺序进行调用的。 -->  
  9. <context-param>    
  10.     <!--配置spring的配置文件  -->  
  11.     <param-name>contextConfigLocation</param-name>    
  12.     <param-value>/WEB-INF/classes/applicationContext.xml</param-value>    
  13. </context-param>    
  14.  <context-param>  
  15.      <!--配置项目运行日志的配置文件  -->  
  16.         <param-name>log4jConfigLocation</param-name>  
  17.         <param-value>/WEB-INF/classes/log4j.properties</param-value>  
  18.     </context-param>   
  19. <listener>    
  20.  <!--配置spring监听文件  -->  
  21.         <listener-class>    
  22.             org.springframework.web.context.ContextLoaderListener    
  23.         </listener-class>    
  24.           
  25. </listener>    
  26.   <listener>  
  27.   <!--配置log4j监听文件  -->  
  28.         <listener-class>  
  29.             org.springframework.web.util.Log4jConfigListener  
  30.         </listener-class>  
  31.     </listener>  
  32.   <filter>      
  33.    <!--配置struts2的配置文件  -->  
  34.     <filter-name>struts2</filter-name>      
  35.     <filter-class>      
  36.         org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter       
  37.     </filter-class>      
  38.      
  39.   </filter>      
  40.    <!--配置struts2过滤 /* 指过滤所有,也可以自行配置过滤特定,默认是action -->  
  41.   <filter-mapping>      
  42.     <filter-name>struts2</filter-name>      
  43.     <url-pattern>/*</url-pattern>      
  44.   </filter-mapping>      
  45.     <welcome-file-list>  
  46.      <!--配置项目起始页面  -->  
  47.         <welcome-file>index.jsp</welcome-file>  
  48.     </welcome-file-list>   
  49. </web-app>    
[html]  view plain  copy
  1. 到这里所有的配置就全部都配置好了,有不明白的可留言,也可私下找我,本人qq2422014433,  
[html]  view plain  copy
  1. 希望对初学者有所帮助!其中有表述不明确的地方,或者错误的地方,请批评改正!  
[html]  view plain  copy
  1. 2.4、其他部分代码  
[html]  view plain  copy
  1. 2.4.1、UserAction.java  
[html]  view plain  copy
  1. <pre name="code" class="html">package wayne.action;  
  2.   
  3. import wayne.dao.UserMapper;  
  4. import wayne.model.User;  
  5.   
  6. import com.opensymphony.xwork2.ActionSupport;  
  7.   
  8. public class UserAction extends ActionSupport{  
  9.     private Integer id;  
  10.     private String name;  
  11.   
  12.     private String password;  
  13.   
  14.     private Byte sex;  
  15.   
  16.     private String phone;  
  17.   
  18.     private String address;  
  19.     private UserMapper userDao;  
  20.       
  21.   
  22.     public UserMapper getUserDao() {  
  23.         return userDao;  
  24.     }  
  25.   
  26.   
  27.     public void setUserDao(UserMapper userDao) {  
  28.         this.userDao = userDao;  
  29.     }  
  30.   
  31.   
  32.   
  33.     public Integer getId() {  
  34.         return id;  
  35.     }  
  36.   
  37.   
  38.     public void setId(Integer id) {  
  39.         this.id = id;  
  40.     }  
  41.   
  42.   
  43.     public String getName() {  
  44.         return name;  
  45.     }  
  46.   
  47.   
  48.     public void setName(String name) {  
  49.         this.name = name;  
  50.     }  
  51.   
  52.   
  53.     public String getPassword() {  
  54.         return password;  
  55.     }  
  56.   
  57.   
  58.     public void setPassword(String password) {  
  59.         this.password = password;  
  60.     }  
  61.   
  62.   
  63.     public Byte getSex() {  
  64.         return sex;  
  65.     }  
  66.   
  67.   
  68.     public void setSex(Byte sex) {  
  69.         this.sex = sex;  
  70.     }  
  71.   
  72.   
  73.     public String getPhone() {  
  74.         return phone;  
  75.     }  
  76.   
  77.   
  78.     public void setPhone(String phone) {  
  79.         this.phone = phone;  
  80.     }  
  81.   
  82.   
  83.     public String getAddress() {  
  84.         return address;  
  85.     }  
  86.   
  87.   
  88.     public void setAddress(String address) {  
  89.         this.address = address;  
  90.     }  
  91.   
  92.   
  93.     /**  
  94.      * 进入开户登记页面  
  95.      *   
  96.      * @return  
  97.      */  
  98.     public String login() {  
  99.         String result ="";  
  100.         try {  
  101.             String username = getName();  
  102.             String password = getPassword();   
  103.             User user = userDao.selectByPrimaryKey(id);  
  104.             if(user!=null){  
  105.                 String pass = user.getPassword();  
  106.                 if(password.equals(pass) && password!=""){  
  107.                     result = "success";  
  108.                 }else{  
  109.                     result = "error";  
  110.                 }  
  111.             }else{  
  112.                 return ERROR;  
  113.             }             
  114.         } catch (Exception e) {  
  115.             e.printStackTrace();  
  116.             return ERROR;  
  117.         }  
  118.         return result;  
  119.     }  
  120. }  

2.4.2、UserMapper.java
[html]  view plain  copy
  1. <pre name="code" class="html">package wayne.dao;  
  2.   
  3. import wayne.model.User;  
  4.   
  5. public interface UserMapper {  
  6.     int deleteByPrimaryKey(Integer id);  
  7.   
  8.     int insert(User record);  
  9.   
  10.     int insertSelective(User record);  
  11.   
  12.     User selectByPrimaryKey(Integer id);  
  13.   
  14.     int updateByPrimaryKeySelective(User record);  
  15.   
  16.     int updateByPrimaryKey(User record);  
  17. }  
[html]  view plain  copy
  1. 2.4.3、User.java  
[html]  view plain  copy
  1. <pre name="code" class="html">package wayne.model;  
  2.   
  3. public class User {  
  4.     private Integer id;  
  5.     private String name;  
  6.   
  7.     private String password;  
  8.   
  9.     private Byte sex;  
  10.   
  11.     private String phone;  
  12.   
  13.     private String address;  
  14.   
  15.     public Integer getId() {  
  16.         return id;  
  17.     }  
  18.   
  19.     public void setId(Integer id) {  
  20.         this.id = id;  
  21.     }  
  22.   
  23.     public String getName() {  
  24.         return name;  
  25.     }  
  26.   
  27.     public void setName(String name) {  
  28.         this.name = name == null ? null : name.trim();  
  29.     }  
  30.   
  31.     public String getPassword() {  
  32.         return password;  
  33.     }  
  34.   
  35.     public void setPassword(String password) {  
  36.         this.password = password == null ? null : password.trim();  
  37.     }  
  38.   
  39.     public Byte getSex() {  
  40.         return sex;  
  41.     }  
  42.   
  43.     public void setSex(Byte sex) {  
  44.         this.sex = sex;  
  45.     }  
  46.   
  47.     public String getPhone() {  
  48.         return phone;  
  49.     }  
  50.   
  51.     public void setPhone(String phone) {  
  52.         this.phone = phone == null ? null : phone.trim();  
  53.     }  
  54.   
  55.     public String getAddress() {  
  56.         return address;  
  57.     }  
  58.   
  59.     public void setAddress(String address) {  
  60.         this.address = address == null ? null : address.trim();  
  61.     }  
  62. }  
2.4.4、UserMapper.xml
[html]  view plain  copy
  1. <pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  3. <mapper namespace="wayne.dao.UserMapper">  
  4.   <resultMap id="BaseResultMap" type="wayne.model.User">  
  5.     <id column="id" jdbcType="INTEGER" property="id" />  
  6.     <result column="name" jdbcType="VARCHAR" property="name" />  
  7.     <result column="password" jdbcType="VARCHAR" property="password" />  
  8.     <result column="sex" jdbcType="TINYINT" property="sex" />  
  9.     <result column="phone" jdbcType="VARCHAR" property="phone" />  
  10.     <result column="address" jdbcType="VARCHAR" property="address" />  
  11.   </resultMap>  
  12.   <sql id="Base_Column_List">  
  13.     id, name, password, sex, phone, address  
  14.   </sql>  
  15.   <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">  
  16.     select   
  17.     <include refid="Base_Column_List" />  
  18.     from user_info  
  19.     where id = #{id,jdbcType=INTEGER}  
  20.   </select>  
  21.   <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">  
  22.     delete from user_info  
  23.     where id = #{id,jdbcType=INTEGER}  
  24.   </delete>  
  25.   <insert id="insert" parameterType="wayne.model.User">  
  26.     insert into user_info (id, name, password,   
  27.       sex, phone, address  
  28.       )  
  29.     values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},   
  30.       #{sex,jdbcType=TINYINT}, #{phone,jdbcType=VARCHAR}, #{address,jdbcType=VARCHAR}  
  31.       )  
  32.   </insert>  
  33.   <insert id="insertSelective" parameterType="wayne.model.User">  
  34.     insert into user_info  
  35.     <trim prefix="(" suffix=")" suffixOverrides=",">  
  36.       <if test="id != null">  
  37.         id,  
  38.       </if>  
  39.       <if test="name != null">  
  40.         name,  
  41.       </if>  
  42.       <if test="password != null">  
  43.         password,  
  44.       </if>  
  45.       <if test="sex != null">  
  46.         sex,  
  47.       </if>  
  48.       <if test="phone != null">  
  49.         phone,  
  50.       </if>  
  51.       <if test="address != null">  
  52.         address,  
  53.       </if>  
  54.     </trim>  
  55.     <trim prefix="values (" suffix=")" suffixOverrides=",">  
  56.       <if test="id != null">  
  57.         #{id,jdbcType=INTEGER},  
  58.       </if>  
  59.       <if test="name != null">  
  60.         #{name,jdbcType=VARCHAR},  
  61.       </if>  
  62.       <if test="password != null">  
  63.         #{password,jdbcType=VARCHAR},  
  64.       </if>  
  65.       <if test="sex != null">  
  66.         #{sex,jdbcType=TINYINT},  
  67.       </if>  
  68.       <if test="phone != null">  
  69.         #{phone,jdbcType=VARCHAR},  
  70.       </if>  
  71.       <if test="address != null">  
  72.         #{address,jdbcType=VARCHAR},  
  73.       </if>  
  74.     </trim>  
  75.   </insert>  
  76.   <update id="updateByPrimaryKeySelective" parameterType="wayne.model.User">  
  77.     update user_info  
  78.     <set>  
  79.       <if test="name != null">  
  80.         name = #{name,jdbcType=VARCHAR},  
  81.       </if>  
  82.       <if test="password != null">  
  83.         password = #{password,jdbcType=VARCHAR},  
  84.       </if>  
  85.       <if test="sex != null">  
  86.         sex = #{sex,jdbcType=TINYINT},  
  87.       </if>  
  88.       <if test="phone != null">  
  89.         phone = #{phone,jdbcType=VARCHAR},  
  90.       </if>  
  91.       <if test="address != null">  
  92.         address = #{address,jdbcType=VARCHAR},  
  93.       </if>  
  94.     </set>  
  95.     where id = #{id,jdbcType=INTEGER}  
  96.   </update>  
  97.   <update id="updateByPrimaryKey" parameterType="wayne.model.User">  
  98.     update user_info  
  99.     set name = #{name,jdbcType=VARCHAR},  
  100.       password = #{password,jdbcType=VARCHAR},  
  101.       sex = #{sex,jdbcType=TINYINT},  
  102.       phone = #{phone,jdbcType=VARCHAR},  
  103.       address = #{address,jdbcType=VARCHAR}  
  104.     where id = #{id,jdbcType=INTEGER}  
  105.   </update>  
  106. </mapper>  
[html]  view plain  copy
  1. 2.4.5index.jsp  
[html]  view plain  copy
  1. <pre name="code" class="html"><%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'index.jsp' starting page</title>  
  13.     <meta http-equiv="pragma" content="no-cache">  
  14.     <meta http-equiv="cache-control" content="no-cache">  
  15.     <meta http-equiv="expires" content="0">      
  16.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  17.     <meta http-equiv="description" content="This is my page">  
  18.     <!-- 
  19.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  20.     -->  
  21.   </head>  
  22.     
  23.   <body>  
  24.     <form action="login" method="post">   
  25.              用    户ID:<input type="text" name="id" />    <!-- 注:这里的name如果设为“id” 则后台struts里必须要配置input -->  
  26.              登陆密码:<input type="password" name="password" />   
  27.     <input type="submit" value="提交">     
  28.     </form>   
  29.   </body>  
  30. </html>  


(注:由于下面代码基本都是Mybatis Generator自动生成的,所以就没详细介绍)
[html]  view plain  copy
  1. 3、常见报错  
[html]  view plain  copy
  1. 3.1、java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for wayne.dao.UserMapper.selectByPrimaryKey  
  2. <span style="white-space:pre">    </span>at org.apache.ibatis.session.Configuration$StrictMap.get(Configuration.java:672)  
  3. <span style="white-space:pre">    </span>at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:507)  
  4. <span style="white-space:pre">    </span>at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:500)  
  5. <span style="white-space:pre">    </span>at org.apache.ibatis.binding.MapperMethod.setupCommandType(MapperMethod.java:240)  
  6. <span style="white-space:pre">    </span>at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:71)  
  7. <span style="white-space:pre">    </span>at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:39)  
  8. <span style="white-space:pre">    </span>at $Proxy7.selectByPrimaryKey(Unknown Source)  
  9. <span style="white-space:pre">    </span>at wayne.action.UserAction.login(UserAction.java:103)  
  10. <span style="white-space:pre">    </span>at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
  11. <span style="white-space:pre">    </span>at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)  
[html]  view plain  copy
  1. 出现这样的报错说明  
[html]  view plain  copy
  1. 3.1.1、mapper.xml中没有加入namespace   
  2. 3.1.2、mapper.xml中的方法和接口mapper的方法不对应   
  3. 3.1.3、mapper.xml没有加入到mybatis-config.xml中(即总的配置文件),例外:配置了mapper文件的包路径的除外   
  4. 3.1.4、mapper.xml文件名和所写的mapper名称不相同。  
[html]  view plain  copy
  1. 3.2另外错误请参考http://wenku.baidu.com/link?url=5bwXDHgjpUAHwbD2TuGofTNpHAyzmTcqhRiBnajzxEjxg7LGfoRcpnU1HW6vP_wWS4eP3n90hd7M_OpiAsow1duJ3vM_Ki63dsAR1Vf-Nyq<span style="font-family:Helvetica, Tahoma, Arial, sans-serif;"><span style="font-size: 14px; line-height: 25.2px;">  
  2. </span></span>4、总结