• SSM学习33:Spring整合MyBatis(重点)


  • <dependencies>
  • <dependency>
  • <groupId>org.springframeworkgroupId>
  • <artifactId>spring-contextartifactId>
  • <version>5.2.10.RELEASEversion>
  • dependency>
  • <dependency>
  • <groupId>com.alibabagroupId>
  • <artifactId>druidartifactId>
  • <version>1.1.16version>
  • dependency>
  • <dependency>
  • <groupId>org.mybatisgroupId>
  • <artifactId>mybatisartifactId>
  • <version>3.5.6version>
  • dependency>
  • <dependency>
  • <groupId>mysqlgroupId>
  • <artifactId>mysql-connector-javaartifactId>
  • <version>5.1.47version>
  • dependency>
  • <dependency>
  • <groupId>org.springframeworkgroupId>
  • <artifactId>spring-jdbcartifactId>
  • <version>5.2.10.RELEASEversion>
  • dependency>
  • <dependency>
  • <groupId>org.mybatisgroupId>
  • <artifactId>mybatis-springartifactId>
  • <version>1.3.0version>
  • dependency>
  • dependencies>
  • project>
  • 数据库准备工作

     

    1.先创建数据库

    1. use mybatis;
    2. create table tbl_account
    3. (
    4. id INT(11),
    5. name VARCHAR(25),
    6. money double
    7. );
    8. insert into tbl_account (id, name, money)
    9. values (1,'郭浩康',100);
    10. select *
    11. from tbl_account;

    2.创建实体类Account.class

    1. package com.itheima.domain;
    2. import java.io.Serializable;
    3. public class Account implements Serializable {
    4. private Integer id;
    5. private String name;
    6. private Double money;
    7. public Integer getId() {
    8. return id;
    9. }
    10. public void setId(Integer id) {
    11. this.id = id;
    12. }
    13. public String getName() {
    14. return name;
    15. }
    16. public void setName(String name) {
    17. this.name = name;
    18. }
    19. public Double getMoney() {
    20. return money;
    21. }
    22. public void setMoney(Double money) {
    23. this.money = money;
    24. }
    25. @Override
    26. public String toString() {
    27. return "Account{" +
    28. "id=" + id +
    29. ", name='" + name + '\'' +
    30. ", money=" + money +
    31. '}';
    32. }
    33. }

     3.创建连接文件jdbc.properties

    1. jdbc.driver=com.mysql.jdbc.Driver
    2. jdbc.url=jdbc:mysql://localhost:3306/mybatis?useSSL=false
    3. jdbc.username=root
    4. jdbc.password=root

    4.创建配置类JdbcConfig.class

    1. package com.itheima.config;
    2. import com.alibaba.druid.pool.DruidDataSource;
    3. import org.springframework.beans.factory.annotation.Value;
    4. import org.springframework.context.annotation.Bean;
    5. import org.springframework.context.annotation.Configuration;
    6. import javax.sql.DataSource;
    7. public class JdbcConfig {
    8. @Value("${jdbc.driver}")
    9. private String driver;
    10. @Value("${jdbc.url}")
    11. private String url;
    12. @Value("${jdbc.username}")
    13. private String userName;
    14. @Value("${jdbc.password}")
    15. private String password;
    16. @Bean
    17. public DataSource dataSource(){
    18. DruidDataSource ds = new DruidDataSource();
    19. ds.setDriverClassName(driver);
    20. ds.setUrl(url);
    21. ds.setUsername(userName);
    22. ds.setPassword(password);
    23. return ds;
    24. }
    25. }

     

    5.MybatisConfig.class

    /*MybatisConfig
    * 连个bean
    * SqlSessionFactoryBean和代理MapperScannerConfigurer
    * */
    1. package com.itheima.config;
    2. import org.mybatis.spring.SqlSessionFactoryBean;
    3. import org.mybatis.spring.mapper.MapperScannerConfigurer;
    4. import org.springframework.context.annotation.Bean;
    5. import javax.sql.DataSource;
    6. /*MybatisConfig
    7. * 连个bean
    8. * SqlSessionFactoryBean和代理MapperScannerConfigurer
    9. * */
    10. public class MybatisConfig {
    11. //定义bean,SqlSessionFactoryBean,用于产生SqlSessionFactory对象
    12. @Bean
    13. public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
    14. SqlSessionFactoryBean ssfb=new SqlSessionFactoryBean();
    15. ssfb.setTypeAliasesPackage("com.itheima.domain");
    16. ssfb.setDataSource(dataSource);
    17. return ssfb;
    18. }
    19. //
    20. //定义bean,返回MapperScannerConfigurer对象
    21. @Bean
    22. public MapperScannerConfigurer mapperScannerConfigurer(){
    23. MapperScannerConfigurer msc = new MapperScannerConfigurer();
    24. msc.setBasePackage("com.itheima.dao");
    25. return msc;
    26. }
    27. }

    Spring准备工作

    1创建MyBatis注解的数据层接口AccountDao.class

    1. package com.itheima.dao;
    2. import com.itheima.domain.Account;
    3. import org.apache.ibatis.annotations.Delete;
    4. import org.apache.ibatis.annotations.Insert;
    5. import org.apache.ibatis.annotations.Select;
    6. import org.apache.ibatis.annotations.Update;
    7. import java.util.List;
    8. public interface AccountDao {
    9. @Insert("insert into tbl_account(name,money)values(#{name},#{money})")
    10. void save(Account account);
    11. @Delete("delete from tbl_account where id = #{id} ")
    12. void delete(Integer id);
    13. @Update("update tbl_account set name = #{name} , money = #{money} where id = #{id} ")
    14. void update(Account account);
    15. @Select("select * from tbl_account")
    16. List findAll();
    17. @Select("select * from tbl_account where id = #{id} ")
    18. Account findById(Integer id);
    19. }

    2.创建业务层接口和实现类

    2.1 AccountService.class

    1. package com.itheima.service;
    2. import com.itheima.domain.Account;
    3. import java.util.List;
    4. public interface AccountService {
    5. void save(Account account);
    6. void delete(Integer id);
    7. void update(Account account);
    8. List findAll();
    9. Account findById(Integer id);
    10. }

    2.2 AccountServiceImpl.class

    1. package com.itheima.service.impl;
    2. import com.itheima.dao.AccountDao;
    3. import com.itheima.domain.Account;
    4. import com.itheima.service.AccountService;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.stereotype.Service;
    7. import java.util.List;
    8. @Service
    9. public class AccountServiceImpl implements AccountService {
    10. @Autowired
    11. private AccountDao accountDao;
    12. public void save(Account account) {
    13. accountDao.save(account);
    14. }
    15. public void update(Account account){
    16. accountDao.update(account);
    17. }
    18. public void delete(Integer id) {
    19. accountDao.delete(id);
    20. }
    21. public Account findById(Integer id) {
    22. return accountDao.findById(id);
    23. }
    24. public List findAll() {
    25. return accountDao.findAll();
    26. }
    27. }

       3. 创建配置类SpringConfig.class

    1. package com.itheima.config;
    2. import org.springframework.context.annotation.ComponentScan;
    3. import org.springframework.context.annotation.Configuration;
    4. import org.springframework.context.annotation.Import;
    5. import org.springframework.context.annotation.PropertySource;
    6. //SpringConfig创建步骤(4步)
    7. // 申明配置类@Configuration
    8. //扫描包@ComponentScan("com.itheima")
    9. //加载jdbc文件@PropertySource("classpath:jdbc.properties")
    10. //导入jdbc类@Import({JdbcConfig.class,MybatisConfig.class})
    11. @Configuration
    12. @ComponentScan("com.itheima")
    13. //@PropertySource:加载类路径jdbc.properties文件
    14. @PropertySource("classpath:jdbc.properties")
    15. @Import({JdbcConfig.class,MybatisConfig.class})
    16. public class SpringConfig {
    17. }

    4.创建测试类App2.class

    1. import com.itheima.config.SpringConfig;
    2. import com.itheima.domain.Account;
    3. import com.itheima.service.AccountService;
    4. import org.springframework.context.ApplicationContext;
    5. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    6. public class App2 {
    7. public static void main(String[] args) {
    8. //加载Spring配置文件
    9. ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
    10. AccountService accountService = ctx.getBean(AccountService.class);
    11. Account ac = accountService.findById(1);
    12. System.out.println(ac);
    13. }
    14. }

  • 相关阅读:
    SSL证书的分类有哪些?如何选择合适的SSL证书?
    【数学+贪心】第十三届蓝桥杯省赛C++ B组《X 进制减法》(C++)
    The Cherno——OpenGL
    Copliot:让你一秒变身网页达人的神奇助手
    IIR滤波器
    Java中如何检测HashMap中是否存在指定Key呢?
    vue02模板语法
    Triton推理服务器吞吐量测试
    Spring Boot 如何快速过滤出一次请求的所有日志?
    【机器学习算法】神经网络与深度学习-9 递归神经网络
  • 原文地址:https://blog.csdn.net/weixin_51330376/article/details/127562191