【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.创建数据库表

create database ssm;
create table account(
    id int primary key auto_increment,
    name varchar(100),
    money double(7,2)
);

 表数据:

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 对应实体类:

public class Account {
    private Integer id;//id
    private String name;//姓名
    private Double money;//余额
}

 ✨5.Mapper接口

public interface AccountMapper {
    public void save(Account account);
    public List<Account> findAll();
}

6.Service接口

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

 ✨7.Service实现

@Service("accountService")
public class AccountServiceImpl implements AccountService {
	public void save(Account account) {
		SqlSession sqlSession = MyBatisUtils.openSession();
		AccountMapper accountMapper = sqlSession.getMapper(AccountMapper.class);
		accountMapper.save(account);
		sqlSession.commit();
		sqlSession.close();
	}

	public List<Account> findAll() {
		SqlSession sqlSession = MyBatisUtils.openSession();
		AccountMapper accountMapper = sqlSession.getMapper(AccountMapper.class);
		return accountMapper.findAll();
	}
}

  ✨8.Controller

@Controller
public class AccountController {
	@Autowired
	private AccountService accountService;

	@RequestMapping("/save")
	@ResponseBody
	public String save(Account account) {
		accountService.save(account);
		return "save success";
	}

	@RequestMapping("/findAll")
	public ModelAndView findAll() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("accountList");
		modelAndView.addObject("accountList", accountService.findAll());
		return modelAndView;
	}
}

 ✨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

  <!--加载propeties文件-->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>

    <!--配置数据源信息-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!--配置sessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!--加载mybatis核心文件-->
        <property name="configLocation" value="classpath:sqlMapConfig-spring.xml"></property>
    </bean>

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

在applicationContext.xml中配置

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

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

在applicationContext.xml中配置

    <!--声明式事务控制-->
    <!--平台事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--配置事务增强-->
    <tx:advice id="txAdvice">
        <tx:attributes>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>

    <!--事务的aop织入-->
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.itheima.service.impl.*.*(..))"></aop:advisor>
    </aop:config>

  ✨5.更新Service实现类代码

@Service("accountService")
public class AccountServiceImpl implements AccountService {
	@Autowired
	private AccountMapper accountMapper;

	public void save(Account account) {
		accountMapper.save(account);
	}

	public List<Account> findAll() {
		return accountMapper.findAll();
	}
}

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

评论 54
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

求不脱发

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

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

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

打赏作者

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

抵扣说明:

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

余额充值