首页 新闻 会员 周边 捐助

自己搭建的一个SSM框架,第一次接触这个东西所以有很多不懂的地方。有些问题请教一下

0
悬赏园豆:10 [待解决问题]

下面是我这个项目的结构,包括什么类目,什么jar包

 

我觉得这个这个东西的难点主要是在于配置文件,所以我先把配置文件贴出来,不知道哪个地方需要改进。配置文件我是直接从另外一个地方考过来改写的,也不知道对错

 

SqlMapConfig.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE configuration
 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 <configuration>
 6 
 7     <!-- 定义别名 -->
 8     <typeAliases>
 9         <package name="com.etc.po" />
10     </typeAliases>
11     <!-- 配置mapper映射文件 -->
12     <mappers>
13         <!-- 加载 原始dao使用映射文件 -->
14         <!-- <mapper resource="sqlmap/User.xml" /> -->
15         
16         <!--批量mapper扫描 遵循规则:将mapper.xml和mapper.java文件放在一个目录 且文件名相同 -->
17         <package name="com.etc.mapper" />
18     </mappers>
19 </configuration>

 

applicationContext-dao.xml

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
 3     xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 6         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
 7         http://www.springframework.org/schema/mvc 
 8         http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
 9         http://www.springframework.org/schema/context 
10         http://www.springframework.org/schema/context/spring-context-3.1.xsd 
11         http://www.springframework.org/schema/aop 
12         http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
13         http://www.springframework.org/schema/tx 
14         http://www.springframework.org/schema/tx/spring-tx-3.1.xsd ">
15 
16 <!-- 配置SqlSessionFactory -->
17 <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
18     <!-- 数据源 -->
19     <property name="dataSource" ref="dataSource"/>
20     <!-- 配置SqlMapConfig.xml -->
21     <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/>
22 </bean>
23 
24 <!-- 配置userDao -->
25 <!-- <bean id="userDao" class="cn.itcast.ssm.dao.old.UserDaoImpl">
26     注入会话工厂
27     <property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
28 </bean> -->
29 
30 
31 <!-- mapper动态代理 -->
32 <!-- 配置userMapper
33 MapperFactoryBean:用于生成 代理对象
34  -->
35 <!-- <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
36    注入会话工厂
37     <property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
38     mapper接口
39     <property name="mapperInterface" value="cn.itcast.ssm.dao.mapper.UserMapper"/>
40 </bean> -->
41 
42 <!-- 使用mapper批量扫描器扫描mapper接口
43 规则:mapper.xml和mapper.java在一个目录 且同名即可 
44 扫描出来mapper,自动让spring容器注册,bean的id就是mapper类名(首字母小写)
45  -->
46 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
47     <!-- 会话工厂 -->
48     <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>
49   <!-- 扫描包路径 
50   多个包中间用半角逗号分隔
51    -->
52     <property name="basePackage" value="com.etc.mapper"/>
53 </bean>
54 
55 
56 </beans>

 

applicationContext-service.xml

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
 3     xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 6         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
 7         http://www.springframework.org/schema/mvc 
 8         http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
 9         http://www.springframework.org/schema/context 
10         http://www.springframework.org/schema/context/spring-context-3.1.xsd 
11         http://www.springframework.org/schema/aop 
12         http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
13         http://www.springframework.org/schema/tx 
14         http://www.springframework.org/schema/tx/spring-tx-3.1.xsd ">
15 
16 
17 <!--  用户管理 -->
18 <bean id="userService" class="com.etc.service.ProductServiceImpl"/>
19 </beans>

 

applicationContext.xml

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
 3     xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 6         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
 7         http://www.springframework.org/schema/mvc 
 8         http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
 9         http://www.springframework.org/schema/context 
10         http://www.springframework.org/schema/context/spring-context-3.1.xsd 
11         http://www.springframework.org/schema/aop 
12         http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
13         http://www.springframework.org/schema/tx 
14         http://www.springframework.org/schema/tx/spring-tx-3.1.xsd ">
15 
16     
17     <!-- 加载配置文件 -->
18     <context:property-placeholder location="classpath:db.properties" />
19     <!-- 使用第三方的数据库连接池dbcp -->
20     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
21         destroy-method="close">
22         <property name="driverClassName" value="${jdbc.driver}" />
23         <property name="url" value="${jdbc.url}" />
24         <property name="username" value="${jdbc.username}" />
25         <property name="password" value="${jdbc.password}" />
26         <!-- 开发阶段数据库最大连接数建议设置小一点够用即可,设置为3 -->
27         <property name="maxActive" value="${jdbc.maxActive}" />
28         <property name="maxIdle" value="${jdbc.maxIdle}" />
29     </bean>
30     
31     <!-- 事务管理 -->
32     
33     <!-- 事务管理器
34     mybatis使用jdbc事务管理
35      -->
36     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
37         <!-- 数据源 -->
38         <property name="dataSource" ref="dataSource"/>
39     </bean>
40     
41     <!-- 通知 -->
42     <tx:advice id="txAdvice" transaction-manager="transactionManager">
43         <!-- 配置传播行为 -->
44         <tx:attributes>
45             <tx:method name="save*" propagation="REQUIRED" />
46             <tx:method name="insert*" propagation="REQUIRED"/>
47             <tx:method name="update*" propagation="REQUIRED"/>
48             <tx:method name="delete*" propagation="REQUIRED"/>
49             <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
50             <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
51             <tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
52         </tx:attributes>
53     </tx:advice>
54     
55     <!-- aop配置 -->
56     <aop:config>
57         <aop:advisor advice-ref="txAdvice"
58          pointcut="execution(* cn.itcast.ssm.service.impl.*.*(..))"/>
59     </aop:config>
60 
61 
62 </beans>

 

 

springmvc.xml

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
 3     xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 6         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
 7         http://www.springframework.org/schema/mvc 
 8         http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
 9         http://www.springframework.org/schema/context 
10         http://www.springframework.org/schema/context/spring-context-3.1.xsd 
11         http://www.springframework.org/schema/aop 
12         http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
13         http://www.springframework.org/schema/tx 
14         http://www.springframework.org/schema/tx/spring-tx-3.1.xsd ">
15 
16     <!-- 组件扫描 只扫描action -->
17     <context:component-scan base-package="com.etc.controller" />
18 
19 
20     <!-- 使用<mvc:annotation-driven />替换上边定义的处理器映射器和适配器 -->
21     <mvc:annotation-driven />
22 
23     <!-- 视图解析器 解析jsp视图,默认使用jstl,要求classpath下有jstl的jar包 -->
24     <bean
25         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
26         <!-- 视图的前缀 -->
27         <property name="prefix" value="/WEB-INF/jsp/" />
28         <!-- 视图的后缀 -->
29         <property name="suffix" value=".jsp" />
30 
31     </bean>
32     
33 
34 </beans>

 

web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 5     id="WebApp_ID" version="2.5">
 6     <display-name>mybatis1218_ssm</display-name>
 7 
 8     <!-- 加载spring容器 -->
 9     <context-param>
10         <param-name>contextConfigLocation</param-name>
11         <param-value>/WEB-INF/classes/spring/applicationContext.xml,/WEB-INF/classes/spring/applicationContext-*.xml</param-value>
12     </context-param>
13     <listener>
14         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
15     </listener>
16 
17     <!-- post乱码处理 -->
18     <filter>
19         <filter-name>CharacterEncodingFilter</filter-name>
20         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
21         <init-param>
22             <param-name>encoding</param-name>
23             <param-value>utf-8</param-value>
24         </init-param>
25     </filter>
26     <filter-mapping>
27         <filter-name>CharacterEncodingFilter</filter-name>
28         <url-pattern>/*</url-pattern>
29     </filter-mapping>
30 
31     <!-- 前端控制器 -->
32     <servlet>
33         <servlet-name>springmvc</servlet-name>
34         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
35         <!-- contextConfigLocation指定 springmvc的全局配置文件 如果 contextConfigLocation不指定,默认找配置文件名称:servlet的name+"-servlet.xml" -->
36         <init-param>
37             <param-name>contextConfigLocation</param-name>
38             <param-value>classpath:spring/springmvc.xml</param-value>
39         </init-param>
40     </servlet>
41 
42     <servlet-mapping>
43         <servlet-name>springmvc</servlet-name>
44         <url-pattern>*.action</url-pattern>
45     </servlet-mapping>
46 
47     <welcome-file-list>
48         <welcome-file>index.html</welcome-file>
49         <welcome-file>index.htm</welcome-file>
50         <welcome-file>index.jsp</welcome-file>
51         <welcome-file>default.html</welcome-file>
52         <welcome-file>default.htm</welcome-file>
53         <welcome-file>default.jsp</welcome-file>
54     </welcome-file-list>
55 </web-app>

 

 

以上是除了db.properties和log4j.properties以外的全部配置文件。下面我把代码贴出来

 

ProductServiceImpl.java

 1 package com.etc.service;
 2 
 3 import java.util.List;
 4 
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 
 7 import com.etc.mapper.ProductMapper;
 8 import com.etc.po.Product;
 9 import com.etc.po.ProductExample;
10 
11 public class ProductServiceImpl implements ProductService{
12 
13     @Autowired
14     private ProductMapper productMapper;
15     @Override
16     public List<Product> getProducts() {
17         // TODO Auto-generated method stub
18         ProductExample example = new ProductExample();
19         example.setDistinct(true);
20         return productMapper.selectByExample(example);
21     }
22 
23     @Override
24     public Product getProductById(int id) {
25         // TODO Auto-generated method stub
26         return productMapper.selectByPrimaryKey(id);
27     }
28 
29 }

这四个是有mybatis生成的,所以应该是不会有错,就没有贴出代码。

 

下面是JUNIT测试类还有控制台的内容,求高手指导

ProductMapperTest.java

 1 package com.etc.test;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 import java.util.List;
 6 
 7 import org.apache.ibatis.io.Resources;
 8 import org.apache.ibatis.session.SqlSession;
 9 import org.apache.ibatis.session.SqlSessionFactory;
10 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
11 import org.junit.Before;
12 import org.junit.Test;
13 
14 import com.etc.mapper.ProductMapper;
15 import com.etc.po.Product;
16 import com.etc.po.ProductExample;
17 
18 public class ProductMapperTest {
19 
20     private SqlSessionFactory sqlSessionFactory;
21 
22     @Before
23     public void setUp() {
24         String resource = "mybatis/SqlMapConfig.xml";
25         InputStream inputStream = null;
26         try {
27             inputStream = Resources.getResourceAsStream(resource);
28         } catch (IOException e) {
29             // TODO Auto-generated catch block
30             e.printStackTrace();
31         }
32         sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
33     }
34 
35     @Test
36     public void test1() {
37         SqlSession sqlSession = sqlSessionFactory.openSession();
38         ProductMapper productMapper = sqlSession.getMapper(ProductMapper.class);
39         Product product = productMapper.selectByPrimaryKey(2);
40         System.out.println(product);
41 
42     }
43     
44     @Test
45     public void test2(){
46         
47         SqlSession sqlSession = sqlSessionFactory.openSession();
48         ProductMapper productMapper = sqlSession.getMapper(ProductMapper.class);
49         
50         ProductExample example = new ProductExample();
51         example.setDistinct(true);
52         List<Product> products = productMapper.selectByExample(example);
53         System.out.println(products.toString());
54         
55         
56         
57     }
58 
59 }

 

用的是MYSQL数据库,字段如图,很简单的一个表

 

源码放到百度云上面共享http://pan.baidu.com/s/1nvlHg6H,坐等高手指导

 

 

 

 

 

远歌的主页 远歌 | 菜鸟二级 | 园豆:210
提问于:2016-04-16 21:45
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册