【SSM】—— Spring+SpringMVC+MyBatis整合使用

目录

SSM整合流程

1、创建工程

2、SSM整合

1)Spring

2)MyBatis

3)SpringMVC

3、功能模块

表与实体类

dao(接口+自动代理)

service(接口+实现类)

controller

4、测试


SSM整合流程

1、创建工程

新建模块可参考:【SpringMVC】—— 如何配置使用SpringMVC(详细步骤)-CSDN博客

pom.xml加入以下坐标


    UTF-8
    1.8
    1.8
    


    
      org.springframework
      spring-webmvc
      5.2.10.RELEASE
    

    
      org.springframework
      spring-jdbc
      5.2.10.RELEASE
    

    
      org.springframework
      spring-test
      5.2.10.RELEASE
    

    
      org.mybatis
      mybatis
      3.5.6
    

    
      org.mybatis
      mybatis-spring
      1.3.0
    

    
      mysql
      mysql-connector-java
      5.1.47
    

    
      com.alibaba
      druid
      1.1.16
    

    
      junit
      junit
      4.12
      test
    

    
      javax.servlet
      javax.servlet-api
      3.1.0
      provided
    

    
      com.fasterxml.jackson.core
      jackson-databind
      2.9.0
    
  
  
    
      
        org.apache.tomcat.maven
        tomcat7-maven-plugin
        2.1
        
          80
          /
        
      
    
  

2、SSM整合

项目目录结构如下:

【SSM】—— Spring+SpringMVC+MyBatis整合使用_第1张图片

1)Spring

SpringConfig

@Configuration
@ComponentScan({"com.spring.service"})
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class, MyBatisConfig.class})
public class SpringConfig {
}

2)MyBatis

MybatisConfig

public class MyBatisConfig {

    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        factoryBean.setTypeAliasesPackage("com.spring.domain");
        return factoryBean;
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setBasePackage("com.spring.dao");
        return msc;
    }
}

JdbcConfig

public class JdbcConfig {

    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;

    @Bean
    public DataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }
}

jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm
jdbc.username=root
jdbc.password=你的密码

3)SpringMVC

ServletConfig

public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
    protected Class[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class};
    }

    protected Class[] getServletConfigClasses() {
        return new Class[]{SpringMvcConfig.class};
    }

    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}

SpringMvcConfig

@Configuration
@ComponentScan("com.spring.controller")
@EnableWebMvc
public class SpringMvcConfig {
}

3、功能模块

表与实体类

数据库建表

create database ssm;
use ssm;
create table tbl_book(
	id int primary key auto_increment,
    type varchar(20),
    name varchar(50),
    description varchar(255)
);
insert into tbl_book values(1,'理论','操作系统','op')

 Book

public class Book {
    private Integer id;
    
    private String type;
    private String name;
    private String description;

    public String toString(){
        return "Book{" +
                "id=" + id +
                ",type='" + type + '\'' +
                ", name='" + name + '\''+
                ",description='" + description + '\''+
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

}

dao(接口+自动代理)

BookDao

public interface BookDao {
    @Insert("insert into tbl_book values(null,#{type},#{name},#{description})")
    public void save(Book book);

    @Update("update tbl_book set type = #{type}, name = #{name}, description = #{description} where id = #{id}")
    public void update(Book book);

    @Delete("delete from tbl_book where id = #{id}")
    public void delete(Integer id);

    @Select("select * from tbl_book where id = #{id}")
    public Book getById(Integer id);

    @Select("select * from tbl_book")
    public List getAll();
}

 

service(接口+实现类)

BookService

public interface BookService {

    /*
    * 保存
    */
    public boolean save(Book book);

    /*
     * 修改
     */
    public boolean update(Book book);

    /*
     * 根据id删除
     */
    public boolean delete(Integer id);

    /*
     * 根据id查询
     */
    public Book getById(Integer id);

    /*
     * 查询全部
     */
    public List getAll();
}

BookServiceImpl

@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookDao bookDao;

    public boolean save(Book book) {
        bookDao.save(book);
        return true;
    }

    public boolean update(Book book) {
        bookDao.update(book);
        return true;
    }

    public boolean delete(Integer id) {
        bookDao.delete(id);
        return true;
    }

    public Book getById(Integer id) {
        return bookDao.getById(id);
    }

    public List getAll() {
        return bookDao.getAll();
    }
}

controller

BookController

@RestController
@RequestMapping("/books")
public class BookController {

    @Autowired
    private BookService bookService;

    @PostMapping
    public boolean save(@RequestBody Book book) {
        return bookService.save(book);
    }

    @PutMapping
    public boolean update(@RequestBody Book book) {
        return bookService.update(book);
    }

    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable Integer id) {
        return bookService.delete(id);
    }

    @GetMapping("/{id}")
    public Book getById(@PathVariable Integer id) {
        return bookService.getById(id);
    }

    @GetMapping
    public List getAll() {
        return bookService.getAll();
    }
}

4、测试

BookServiceTest

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class BookServiceTest {

    @Autowired
    private BookService bookService;

    @Test
    public void testGetById(){
        Book book = bookService.getById(1);
        System.out.println(book);
    }

    @Test
    public void testGetAll(){
        List all = bookService.getAll();
        System.out.println(all);
    }
}


你可能感兴趣的:(Spring,spring,mybatis,java,ssm)