以一个从数据库中查询数据为例,来一步一步总结ssm整合内容!会用到注解+xml的方式
文章目录
一、dao层
1、编写实体类
2、编写dao接口(MyBatis框架不用写实现类)
public interface ItemsDao {
@Select("select * from items where id = #{id}")
Items findById(Integer id);
}
3、编写xml文件(不再写SqlMapConfig.xml,直接写到applicationContext.xml里面)
<!--dao层配置文件开始-->
<!--配置数据库连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/ssm"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!--配置生产SqlSession对象的工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--扫描接口包路径,生成包下所有的接口代理对象,并放入spring容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zy.dao"/>
</bean>
<!--dao层配置文件结束-->
4、dao层编写完毕,进行单元测试
//获取spring容器
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//dao测试
//从容器中拿到所需的dao的代理对象
ItemsDao itemsDao = ac.getBean(ItemsDao.class);
//ItemsDao itemsDao = ac.getBean("itemsDao");
//调用方法
Items items = itemsDao.findById(1);
System.out.println(items.getName());
二、service层
1、编写service接口和实现类
public interface ItemsService {
Items findById(Integer id);
}
@Service("itemsService")
public class ItemsServiceImpl implements ItemsService {
@Autowired
private ItemsDao itemsDao;
public Items findById(Integer id) {
return itemsDao.findById(id);
}
}
2、编写xml文件(applicationContext.xml)
<!--service层配置文件开始-->
<!--组件扫描配置-->
<context:component-scan base-package="com.zy.service"/>
<!-- spring中基于XML的声明式事务控制配置步骤
1、配置事务管理器
2、配置事务的通知
此时我们需要导入事务的约束 tx名称空间和约束,同时也需要aop的
使用tx:advice标签配置事务通知
属性:
id:给事务通知起一个唯一标识
transaction-manager:给事务通知提供一个事务管理器引用
3、配置AOP中的通用切入点表达式
4、建立事务通知和切入点表达式的对应关系
5、配置事务的属性
是在事务的通知tx:advice标签的内部
-->
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置事务的通知-->
<tx:advice id="advice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="*" propagation="REQUIRED" read-only="false"/>
</tx:attributes>
</tx:advice>
<!--配置切面-->
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* com.zy.service.impl.*.*(..))"/>
<aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>
</aop:config>
<!--service层配置文件结束-->
3、service层编写完毕,进行单元测试
//获取spring容器
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//service测试
ItemsService itemsService = (ItemsService) ac.getBean("itemsService");
//ItemsService itemsService = ac.getBean(ItemsService.class);
Items items1 = itemsService.findById(1);
System.out.println(items1.getName());
三、web层
1、编写index.jsp和itemsDetail.jsp
index.jsp
<body>
<a href="/items/findDetail">查询详细信息</a>
</body>
itemsDetail.jsp
<body>
<form>
<table width="100%" border=1>
<tr>
<td>商品名称</td>
<td> ${items.name } </td>
</tr>
<tr>
<td>商品价格</td>
<td> ${items.price } </td>
</tr>
<tr>
<td>商品简介</td>
<td>${items.detail} </td>
</tr>
</table>
</form>
</body>
2、编写controller类
@Controller
@RequestMapping("/items")
public class ItemsController {
@Autowired
private ItemsService itemsService;
@RequestMapping("/findDetail")
public String findDetail(Model model){
Items items = itemsService.findById(1);
model.addAttribute("items",items);
return "itemsDetail";
}
}
3、编写springmvc.xml
<!--组件扫描-->
<context:component-scan base-package="com.zy.controller"/>
<!--处理器映射器,处理器适配器-->
<mvc:annotation-driven/>
<!--视图解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--释放静态资源-->
<mvc:default-servlet-handler/>
4、编写web.xml
<web-app>
<!--编码过滤器-->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置spring核心监听器,默认只加载WEB-INF目录下的applicationContext.xml配置文件-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--重新指定spring配置文件的路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--springmvc的核心servlet-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>