• 【SSM直击大厂】最终章:SSM 整合


    🙊🙊作者主页:🔗求不脱发的博客

    📔📔 精选专栏:🔗SSM直击大厂

    📋📋 精彩摘要学完了整个Spring+SpringMVC+Mybatis基础知识,最后将三者整合到一起共同发挥作用才是最重要的。其中SSM整合的实质,仅仅就是将Mybatis整合入Spring。因为SpringMVC原本就是Spring的一部分,不用专门整合。

    💞💞觉得文章还不错的话欢迎大家点赞👍➕收藏⭐️➕评论💬支持博主🤞


    📚目录

    📖SSM 整合

    📝1️⃣原始整合方式

     ✨1.创建数据库表

     ✨2.创建Maven工程

     ✨3.配置pom.xml坐标

     ✨4.实体类

     ✨5.Mapper接口

     ✨6.Service接口

     ✨7.Service实现

     ✨8.Controller

     ✨9.相关配置文件

    📝2️⃣Spring整合MyBatis

     ✨1.整合思路

     ✨2.将SqlSessionFactory配置到Spring容器中

     ✨3.扫描Mapper,让Spring容器产生Mapper实现类

     ✨4.配置声明式事务控制

     ✨5.更新Service实现类代码

    📝【SSM直击大厂】完结散花


    📖SSM 整合


    📝1️⃣原始整合方式

    1.创建数据库表

    1. create database ssm;
    2. create table account(
    3. id int primary key auto_increment,
    4. name varchar(100),
    5. money double(7,2)
    6. );

     表数据:

    idnamomoney
    1tom5000
    2lucy5000

    2.创建Maven工程

    基本项目结构:

     ✨3.配置pom.xml坐标

    相关依赖:

    这里不再详细列出具体代码,其中主要包括以下相关依赖(简单列出artifactId)。

    <!--spring相关-->
      <artifactId>spring-context</artifactId>
      <artifactId>aspectjweaver</artifactId>
      <artifactId>spring-jdbc</artifactId>
      <artifactId>spring-tx</artifactId>
      <artifactId>spring-test</artifactId>
      <artifactId>spring-webmvc</artifactId>
    
    <!--servlet和jsp-->
      <artifactId>servlet-api</artifactId>
      <artifactId>jsp-api</artifactId>
    
    <!--mybatis相关-->
      <artifactId>mybatis</artifactId>
      <artifactId>mybatis-spring</artifactId>
      <artifactId>mybatis-plus</artifactId>
      <artifactId>lombok</artifactId>
      <artifactId>mysql-connector-java</artifactId>
      <artifactId>c3p0</artifactId>

     ✨4.实体类

    数据库表 account 对应实体类:

    1. public class Account {
    2. private Integer id;//id
    3. private String name;//姓名
    4. private Double money;//余额
    5. }

     ✨5.Mapper接口

    1. public interface AccountMapper {
    2. public void save(Account account);
    3. public List<Account> findAll();
    4. }

    6.Service接口

    1. public interface AccountService {
    2. void save(Account account); //保存账户数据
    3. List<Account> findAll(); //查询账户数据
    4. }

     ✨7.Service实现

    1. @Service("accountService")
    2. public class AccountServiceImpl implements AccountService {
    3. public void save(Account account) {
    4. SqlSession sqlSession = MyBatisUtils.openSession();
    5. AccountMapper accountMapper = sqlSession.getMapper(AccountMapper.class);
    6. accountMapper.save(account);
    7. sqlSession.commit();
    8. sqlSession.close();
    9. }
    10. public List<Account> findAll() {
    11. SqlSession sqlSession = MyBatisUtils.openSession();
    12. AccountMapper accountMapper = sqlSession.getMapper(AccountMapper.class);
    13. return accountMapper.findAll();
    14. }
    15. }

      ✨8.Controller

    1. @Controller
    2. public class AccountController {
    3. @Autowired
    4. private AccountService accountService;
    5. @RequestMapping("/save")
    6. @ResponseBody
    7. public String save(Account account) {
    8. accountService.save(account);
    9. return "save success";
    10. }
    11. @RequestMapping("/findAll")
    12. public ModelAndView findAll() {
    13. ModelAndView modelAndView = new ModelAndView();
    14. modelAndView.setViewName("accountList");
    15. modelAndView.addObject("accountList", accountService.findAll());
    16. return modelAndView;
    17. }
    18. }

     ✨9.相关配置文件

    • Spring配置文件:applicationContext.xml
    • SprngMVC配置文件:spring-mvc.xml
    • MyBatis映射文件:AccountMapper.xml
    • MyBatis核心文件:sqlMapConfig.xml
    • 数据库连接信息文件:jdbc.properties
    • Web.xml文件:web.xml
    • 日志文件:log4j.xml

    📝2️⃣Spring整合MyBatis

     ✨1.整合思路

    对于下面部分代码:

    SqlSession sqlSession = MyBatisUtils.openSession();
            AccountMapper accountMapper = sqlSession.getMapper(AccountMapper.class);
            accountMapper.save(account);
            sqlSession.commit();
            sqlSession.close();

    可将Session工厂交给Spring容器管理,从容器中获得执行操作的Mapper实例。

    将事务的控制交给Spring容器使用声明式事务控制。


    2.将SqlSessionFactory配置到Spring容器中

    在applicationContext.xml中配置Bean

    1. <!--加载propeties文件-->
    2. <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
    3. <!--配置数据源信息-->
    4. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    5. <property name="driverClass" value="${jdbc.driver}"></property>
    6. <property name="jdbcUrl" value="${jdbc.url}"></property>
    7. <property name="user" value="${jdbc.username}"></property>
    8. <property name="password" value="${jdbc.password}"></property>
    9. </bean>
    10. <!--配置sessionFactory-->
    11. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    12. <property name="dataSource" ref="dataSource"></property>
    13. <!--加载mybatis核心文件-->
    14. <property name="configLocation" value="classpath:sqlMapConfig-spring.xml"></property>
    15. </bean>

     ✨3.扫描Mapper,让Spring容器产生Mapper实现类

    在applicationContext.xml中配置

    1. <!--扫描mapper所在的包 为mapper创建实现类-->
    2. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    3. <property name="basePackage" value="com.ssm.mapper"></property>
    4. </bean>

     ✨4.配置声明式事务控制

    在applicationContext.xml中配置

    1. <!--声明式事务控制-->
    2. <!--平台事务管理器-->
    3. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    4. <property name="dataSource" ref="dataSource"></property>
    5. </bean>
    6. <!--配置事务增强-->
    7. <tx:advice id="txAdvice">
    8. <tx:attributes>
    9. <tx:method name="*"/>
    10. </tx:attributes>
    11. </tx:advice>
    12. <!--事务的aop织入-->
    13. <aop:config>
    14. <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.itheima.service.impl.*.*(..))"></aop:advisor>
    15. </aop:config>

      ✨5.更新Service实现类代码

    1. @Service("accountService")
    2. public class AccountServiceImpl implements AccountService {
    3. @Autowired
    4. private AccountMapper accountMapper;
    5. public void save(Account account) {
    6. accountMapper.save(account);
    7. }
    8. public List<Account> findAll() {
    9. return accountMapper.findAll();
    10. }
    11. }

    📝【SSM直击大厂】完结散花

  • 相关阅读:
    【编程题】【Scratch四级】2022.06 判断闰年
    推荐前 6 名 JavaScript 和 HTML5 游戏引擎
    JuiceFS 在多云存储架构中的应用 | 深势科技分享
    【校招VIP】产品深入分析之电商运营
    PHP伪协议详解
    OpenAI官方吴达恩《ChatGPT Prompt Engineering 提示词工程师》(7)聊天机器人 / ChatBot
    音视频开发常用名词解释
    大数据架构系列:如何理解湖仓一体?
    【编程题 】 CD108 反转部分单向链表(详细注释 易懂)
    Kafka MQ 如何处理请求
  • 原文地址:https://blog.csdn.net/qq_52360069/article/details/124886664