springMVC实例

一、项目准备

   jar包:

     mybatis-spring-1.0.1.jar

     mybatis-3.0.5.jars

     spring3.1下的必须包

     数据库驱动

     其他一些零零碎碎的jar包(这些大家都懂的吧)

 

   数据表

   CREATE TABLE `notice` (
  `Author` varchar(255) default NULL,
  `Content` varchar(255) default NULL,
  `Title` varchar(255) default NULL,
  `AddTime` datetime default NULL,
  `NoticeId` int(11) NOT NULL auto_increment,
  PRIMARY KEY  (`NoticeId`)
  ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

 

二、各配置文件

  1. mybatis-config.xml

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration  PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"  "http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
 <typeAliases>
  <typeAlias type="com.huaxin.bean.Notice" alias="notice" />
 </typeAliases>
 <mappers>
  <mapper resource="com/huaxin/bean/notice-mapper.xml" />
 </mappers>
</configuration>

 

2.Notice.java

 

public class Notice implements java.io.Serializable {

 private Integer noticeId;
 private String author;
 private Timestamp addTime;
 private String content;
 private String title;

 public Notice() {
 }

 public String getTitle() {
  return title;
 }

 public void setTitle(String title) {
  this.title = title;
 }

 public Integer getNoticeId() {
  return this.noticeId;
 }

 public void setNoticeId(Integer noticeId) {
  this.noticeId = noticeId;
 }

 public String getAuthor() {
  return this.author;
 }

 public void setAuthor(String author) {
  this.author = author;
 }

 public Timestamp getAddTime() {
  return this.addTime;
 }

 public void setAddTime(Timestamp addTime) {
  this.addTime = addTime;
 }

 public String getContent() {
  return this.content;
 }

 public void setContent(String content) {
  this.content = content;
 }

}

 

3.notice-mapper.xml

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"   
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">

<mapper namespace="com.huaxin.dao.NoticeDao">
 
 <resultMap type="notice" id="noticeResult">
  <result column="NoticeId" property="noticeId" />
  <result column="Title" property="title" />
  <result column="AddTime" property="addTime" />
 </resultMap>
 
 <select id="get" parameterType="int" resultType="notice" resultMap="noticeResult">
  SELECT * FROM notice WHERE NoticeId = #{noticeId} 
 </select>
 <insert id="save" parameterType="notice">
  INSERT INTO notice (NoticeId, Title, AddTime) values (#{noticeId}, #{title}, #{addTime})   
 </insert>
 <delete id="delete" parameterType="int">
  DELETE FROM notice WHERE NoticeId = #{noticeId} 
 </delete>
 
</mapper>

 

4.spring配置文件

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
 <bean id="placeholderConfig"
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="location">
   <value>classpath:init.properties</value>
  </property>
 </bean>
 <bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"
  destroy-method="close" dependency-check="none">
  <property name="driverClass">
   <value>${datasource.driverClassName}</value>
  </property>
  <property name="jdbcUrl">
   <value>${datasource.url}</value>
  </property>
  <property name="username">
   <value>${datasource.username}</value>
  </property>
  <property name="password">
   <value>${datasource.password}</value>
  </property>

  <property name="idleConnectionTestPeriod">
   <value>${boneCP.idleConnectionTestPeriod}</value>
  </property>

  <property name="idleMaxAge">
   <value>${boneCP.idleMaxAge}</value>
  </property>
  <property name="maxConnectionsPerPartition">
   <value>${boneCP.maxConnectionsPerPartition}</value>
  </property>
  <property name="minConnectionsPerPartition">
   <value>${boneCP.minConnectionsPerPartition}</value>
  </property>
  <property name="partitionCount">
   <value>${boneCP.partitionCount}</value>
  </property>
  <property name="acquireIncrement">
   <value>${boneCP.acquireIncrement}</value>
  </property>
  <property name="statementsCacheSize">
   <value>${boneCP.statementsCacheSize}</value>
  </property>
  <property name="statementsCachedPerConnection">
   <value>${boneCP.statementsCachedPerConnection}</value>
  </property>
  <property name="releaseHelperThreads">
   <value>${boneCP.releaseHelperThreads}</value>
  </property>
 </bean>
 
 <bean id="htm"
  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <!-- datasource 配置jdbcTemplate -->
  <property name="dataSource">
   <ref local="dataSource" />
  </property>
 </bean>

 <bean id="txProxyTemplate" abstract="true"
  class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  <property name="transactionManager">
   <ref bean="htm" />
  </property>
  <property name="transactionAttributes">
   <props>
    <prop key="create*">
     PROPAGATION_REQUIRED,-com.ebay.exception.ApplyException
    </prop>
    <prop key="save*">
     PROPAGATION_REQUIRED,-com.ebay.exception.ApplyException
    </prop>
    <prop key="remove*">
     PROPAGATION_REQUIRED,-com.ebay.exception.ApplyException
    </prop>
    <prop key="update*">
     PROPAGATION_REQUIRED,-com.ebay.exception.ApplyException
    </prop>
    <prop key="delete*">
     PROPAGATION_REQUIRED,-com.ebay.exception.ApplyException
    </prop>
    <prop key="batch*">
     PROPAGATION_REQUIRED,-com.ebay.exception.ApplyException
    </prop>
    <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
   </props>
  </property>
 </bean>

<!-- ibatis配置 -->
  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
   <!-- <property name="mapperLocations" value="classpath:sample/config/mappers/**/*.xml" -->
    <property name="configLocation" value="classpath:mybatis-config.xml" />
    <property name="dataSource" ref="dataSource" />
  </bean>
 
  <!--
  <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
   <constructor-arg index="0" ref="sqlSessionFactory" />
  </bean>
   -->
 
 <bean abstract="true" lazy-init="true" id="ibatis">
  <property name="sqlSessionFactory" ref="sqlSessionFactory" />
 </bean>
 
  <!-- ==================== End =================== -->
 <bean id="noticeService" parent="txProxyTemplate">
  <property name="target">
   <bean class="com.huaxin.service.NoticeServiceImpl">
    <property name="noticeDao" ref="noticeDao" />
   </bean>
  </property>
 </bean>
 <bean id="noticeDao" parent="ibatis" class="com.huaxin.dao.NoticeDaoImpl"/>
 
 
</beans>

 

5.测试

public class NoticeDaoImpl extends SqlSessionDaoSupport implements NoticeDao {
      //SqlSessionDaoSupport里面有SqlSessionFactory属性
 public Notice get(Integer id) {
  return (Notice) this.getSqlSession().selectOne("com.huaxin.dao.NoticeDao.get", id);
 }

}

你可能感兴趣的:(springMVC)