SSM合成

1.我们已经对spirng,mybatis,springmvc有了一定的了解。
所以我们今天打算提前进行ssm合成。
2.SSM合成
SSM 整合
步骤:
0. 创建maven项目
0.配置本地maven
1.添加web

  1. 添加依赖
    1.servlet-api
    2.jsp-api
    3.mysql
    4.mybatis
    5.druid
    6.mybatis-spring
    7.fileupload
    8.io
    9.jstl
    10.junit
    11.spring context beans core web webmvc aop aspectj jdbc
    12.log4j
    13.分页插件
  2. 创建、编写 SSM 整合配置文件
    1. web.xml(声明当前项目使用Spring完成整合管理、指明整合配置文件的名称和位置)
      1.spring
       <context-param>
       	<param-name>contextConfigLocation</param-name>
      
       <param-value>classpath:spring/spring-config.xml</param-value>
       </context-param>
      
       <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
       <!-- 默认整合配置文件: /WEB-INF/applicationContext.xml, 可以自行指定 -->
       </listener>
      
      classpath:spring/spring-config.xml
      2.springmvc
       <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-servlet.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>
      
      3.乱码过滤器

      enconding
      org.springframework.web.filter.CharacterEncodingFilter

      encoding
      utf-8


      forceEncoding
      true



      enconding
      /*
    2. 整合配置文件(Controller、Biz、Dao)
      new file - 加入 schema
      <?xml version="1.0" encoding="UTF-8"?>


      Dao - MyBatis
      0 - 配置资源文件dbconfig.properties
      # 1 - 数据库连接信息
      driver = com.mysql.cj.jdbc.Driver
      url = jdbc:mysql://127.0.0.1:3306/mydb?serverTimezone=UTC&useSSL=false
      usn = root
      pwd = root
      # 2 - 声明数据库连接池的配置信息
      initialSize = 2
      maxActive = 20
      maxIdle = 20
      minIdle = 5
      maxWait = 60000
      扫描包
      <context:component-scan base-package=“com.tjetc.dao”></context:component-scan>
      1 - 读入数据库属性配置文件




      2 - 声明、配置 dataSource












      3 - 声明、配置 sqlSessionFactory
      其中,指明 MyBatis 的配置
      建议: 所有的Dao == XxxMapper
      所有的映射配置文件 == XxxMapper.xml





    3. MyBatis 配置
      0. 数据库配置的属性配置文件
      配置数据库连接、数据库连接池的属性
      1. 系统|全局配置文件
        —XXX
      2. 映射配置文件
        —====





    4.springmvc配置文件 <?xml version="1.0" encoding="UTF-8"?>



mvc:annotation-driven/

<context:component-scan base-package=“com.tjetc.contoller”/>
<context:component-scan base-package=“com.tjetc.service”/>











3. 实现 Dao(MyBatis 功能)
1 - 完成 po
2 - Mapper.xml
3 - Mapper接口(Dao)
@Repository
public interface BookMapper {
Book findById(Integer id);
List findAll();
}

  1. 完成 SSM 注入
    1 - 声明 Dao 注解、并实现注入
    2 - 声明 Service 注解,并实现注入
    0.接口:
    public interface BookService {
    public List getAllBooks();
    }
    1.impl:
    @Service
    public class BookServiceImpl implements BookService {
    @Autowired
    private BookMapper bookMapper;
    public List getAllBooks() {
    // TODO Auto-generated method stub
    List books = bookMapper.findAll();
    return books;
    }
    }
    3 - 声明 controller注解,并实现注入。
    5.事物管理
    1.加入条目
    xmlns:aop=“http://www.springframework.org/schema/aop”
    xmlns:tx=“http://www.springframework.org/schema/tx”

    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    2.配置事物管理器



     <tx:advice id="txadvice" transaction-manager="transactionManager" >
     <tx:attributes>
     	<tx:method name="add*" read-only="true"/>
     </tx:attributes>
    

    </tx:advice>

    aop:config

     <aop:pointcut id="pointcut" expression="execution(* com.tjetc.service..*(..))"/>
     <!-- 切面 -->
     <aop:advisor advice-ref="txadvice" pointcut-ref="pointcut"></aop:advisor>
    

    </aop:config>

    <tx:annotation-driven transaction-manager=“transactionManager” />
    3.在需要执行事物管理service实现类中的方法上加上@Transactional

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZZBvos

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值