`
medbricom
  • 浏览: 104738 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Spring3.0MVC+MyBatis3.0+Spring3.0(全注解列子)

阅读更多
说明:
     附件是项目截图及所需包截图
     此项目在tomcat,weblogic10下测试通过
配置文件
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 加载Spring容器配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 设置Spring容器加载配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com/medbri/mss/config/applicationContext-*.xml</param-value>
</context-param>
<!-- 配置SpringMvc核心控制器 -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com/medbri/mss/config/applicationContext-*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 解决工程编码过滤器 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

applicationContext-common.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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="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/jee
http://www.springframework.org/schema/jee/spring-jee-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"
default-autowire="byName" default-lazy-init="true">

<!-- 定义受环境影响易变的变量 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:com/medbri/mss/config/jdbc.properties</value>
</list>
</property>
</bean>

<!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
<context:component-scan base-package="com.medbri.mss" />

<!-- 数据源配置,使用应用内的DBCP数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- Connection Info -->
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />

<!-- Connection Pooling Info -->
<property name="initialSize" value="5" />
<property name="maxActive" value="100" />
<property name="maxIdle" value="30" />
<property name="maxWait" value="500" />
<property name="defaultAutoCommit" value="false" />
</bean>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:com/medbri/mss/config/mybatis-config.xml" />
<property name="dataSource" ref="dataSource" />
</bean>

<!-- 事务管理器配置,单数据源事务 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

<!-- 通过扫描的模式,扫描目录在com/medbri/mss/mapper目录下,所有的mapper都继承SqlMapper接口 这样一个bean就可以了   -->
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.medbri.mss.mapper"/>
        <property name="markerInterface" value="com.medbri.mss.utils.SqlMapper"/>
    </bean>

<aop:config proxy-target-class="true">
<aop:advisor pointcut="execution(* com.medbri.mss.service..*Service.*(..))"
advice-ref="txAdvice" />
<aop:advisor pointcut="execution(* com.medbri.mss.utils..*Service.*(..))"
advice-ref="txAdvice" />
</aop:config>

<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="query*" read-only="true" />
<tx:method name="is*" read-only="true" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
</beans>
applicationContext-mvc.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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="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/jee
http://www.springframework.org/schema/jee/spring-jee-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"
default-autowire="byName" default-lazy-init="true">

    <!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
<context:component-scan base-package="com.medbri.mss" />
    <!--  annotation默认的方法映射适配器 -->
    <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    <!--启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
    <bean id="handlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
      <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>
jdbc.properties


jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc\:oracle\:thin\:@localhost\:1521\:XE
jdbc.username=project
jdbc.password=123

mybatis-config.xml
<?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>
</configuration>
AccountMapperController.java
@Controller
@RequestMapping("/accounts")
public class AccountMapperController{
protected static final Logger logger = Logger.getLogger(AccountMapperController.class);
protected static final Integer PAGE_SIZE = 10;
    @Inject
    private AccountMapper accountMapper;
/**
* 转向用户添加页面
* @return 返回转向信息
*/
@RequestMapping("/addUser")
public String addUser() {
return "addUser";
}
   
/**
* 添加用户
* @param acc实体
* @return 返回转向信息
*/
@RequestMapping("/add")
public String add(Account acc) {
try {
accountMapper.add(acc);
} catch (Exception e) {
e.printStackTrace();
logger.error("增加时发生异常:",e);
}
return "redirect:/accounts/queryPage.do";
}

/**
* 查询用户
* @param acc实体
* @return 返回转向信息
*/
@RequestMapping("/queryPage")
public String queryPage(Account acc,Model model) {
try {
int count =accountMapper.getCount(acc);
Pagenation pagenation=new    Pagenation(PAGE_SIZE,acc.getPageNum(),count);
acc.setStartRow(pagenation.getStartRow());
acc.setPageSize(PAGE_SIZE);
List<Account> list=accountMapper.getAllList(acc);
pagenation.setList(list);
model.addAttribute("pagenation",pagenation);
} catch (Exception e) {
e.printStackTrace();
logger.error("query时发生异常:",e);
}
return "userList";
}
}

AccountMapper.java
public interface AccountMapper extends SqlMapper {
/**
* 添加
*
* @param account
*            实体
* @throws Exception
*             抛出异常
*/
@Insert("insert into users values(seq_user_id.nextval,#{userName},#{userPassword})")
public void add(Account account) throws Exception;

/**
* 修改
*
* @param classMethod
*            mybatis配置文件里面对应的命名空间+要执行的sql语句id
* @param entity
*            封装数据的实体
* @return 返回操作结果
* @throws Exception
*             抛出所有异常
*/
@Update("update users set userName=#{userName},userPassword=#{userPassword} where userId=#{userId}")
public void edit(Account account) throws Exception;

/**
* 删除
*
* @param entity
*            封装数据的实体
* @return 返回操作结果
* @throws Exception
*             抛出所有异常
*/
@Delete("delete from users where userId=#{userId}")
public void remvoe(Account account) throws Exception;

/**
* 以id为条件查找对象
*
* @param entity
*            封装数据的实体
* @return 返回查询结果
* @throws Exception
*             抛出所有异常
*/
@Select("select t.userId,t.userName,t.userPassword from users t where t.userId=#{userId}")
public Account get(Account account) throws Exception;

/**
* 查询
*
* @param entity
*            封装数据的实体
* @return 返回查询结果
* @throws Exception
*             抛出所有异常
*/
@Select("select * from(select a.*,rownum r from(select t.userId userId,t.userName userName,t.userPassword userPassword from users t)a ) where r > #{startRow} and rownum <= #{pageSize}")
public List<Account> getAllList(Account account) throws Exception;

/**
* 查询数量
*
* @param entity
*            封装数据的实体
* @return 返回查询结果
* @throws Exception
*             抛出所有异常
*/
@Select("select count(1)from users")
public int getCount(Account account) throws Exception;
}
SqlMapper.java
public interface SqlMapper {

}
  • 大小: 56.4 KB
  • 大小: 97.5 KB
  • 大小: 23.6 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics