SMM整合
SSM整合流程
1.创建工程
2.SSM整合
- Spring
-SpringConfig - Mybaitis
-MybatisConfig、JdbcConfig、jdbc.properties - SpringMVC
-ServletConfig、SpringMvcConfig
3.功能模块
- 表与实体类
- dao(接口+自动代理)
- service(接口+实现类)
- controller
图书馆SSM整合Demo
Spring整合Mybatis
- 配置
-SpringConfig
-JDVCconfig、jdbc.properties
-MyBatisConfig
@Configuration
@ComponentScan("com.mei")
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class,MybatisConfig.class})
public class SpringConfig{
}
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 ds = new DruidDataSource();
ds.setDriverClassName(driver);
ds.setUrl(url);
ds.setUsername(userName);
ds.setPassword(passWord);
return ds;
}
}
public class MybaitisConfig{
@Bean
public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
SqlsessionFactory ssfb = new SqlSessionFactoryBean();
ssfb.setTypeAliasesPackage("com.mei.domain");
ssfb.setDataSource(dataSource);
return ssfb;
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
MapperScannerConfigurer msc = new MapperScannerConfigurer();
msc.setBasePackage("com.mei.dao")
retrun msc;
}
}
- 模型
-Book
public class Book{
private Integer id;
private String type;
private String name;
private String description;
}
- 数据层标准开发
-BookDao
public interface BookDao{
@Insert("insert into tbl_book(type,name,description)values(#{type},#{name},#{description})")
void save (Book book);
@Delete("delete from tbl_book where id = #{id}")
void delete(Integer id);
@Update("update tbl_book set type=#{type},name = #{name},description = #{description} where id = #{id}")
void update(Book book)
@Select("select * from tbl_book")
List<Book> getAll();
@Select("select * from tbl_book where id = #{id}")
Book getById(Integer id);
}
- 业务层标准开发
-BookService
-BookserviceImpl
public interface BookService {
void save (Book book);
void delete (Integer id);
void update (Book book);
List<Book> getAll();
Book getById (Integer id);
}
@Service
public class BookServiceImpl implements BookService{
@Autowired
private BookDao bookDao;
public void save(Book book){ bookDao.save(book); }
public void update(Book book){ bookDao.update(book); }
public void delete(Integer id){ bookDao.delete(id); }
public Book getById(Integer id){ return bookDao.getByid(id); }
public List<Book> getAll(){ return bookDao.getAll(); }
}
- 测试接口
-BookServiceTest
@RunWith(SpringJunit4ClassRunner.calss)
@ContextConfiguration(classes = SpringConfig.class)
public class BookserviceTest {
@Autowired
private BookService bookService;
@Test
public void testGetById(){
System.out.println(bookService.getById(1));
}
@Test
public void testGetAll(){
System.out.println(bookService.getAll());
}
}
- 事务处理
public class JdbcConfig {
@Bean
piblic PlatformTransactionManager transactionManager(DataSource dataSource){
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(dataSource);
return transactionManager;
}
}
@Configuration
@ComponentScan("com.mei")
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class,MybatisConfig.class})
@EnableTransactionManagement
public class SpringConfig{
}
@Transactional
public interface BookService{
}
Spring整合SpringMVC
- web配置类
public calss ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer{
protected Class<?>[] getRootConfigClasses() {
return new Class[]{SpringConfig.calss};
}
protected Clss<?>[] getRootConfigClasses() {
return new Class[]{SpringMvcConfig.class}
}
protected String[] getServletMappinvgs{} {
return new String[]{"/"}
}
//乱码处理
@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter filter = new CharacterEncodingFilter();
filter.setEncoding("UTF-8");
return new Filter[]{filter};
}
}
- SpringMVC配置类
@Configuration
@ComponentScan("com.mei.controller")
@EnableWenMvc
public class SpringMvcConfig {
}
- 基于Restful的Controller开发
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private BookService bookService;
@PostMapping
public void save(@RequestBody Book book) {
bookService.save(book);
}
@PutMapping
public void update(@RequestBody Book book) {
bookService.update(book);
}
@DeleteMapping("/{id}")
public void delete (@PathVariable Integer id) {
bookService.delete(id);
}
@GetMapping("/{id}")
public Book getById(@PathVariable Integer id){
return bookService.getById(id);
}
@GetMapping
public List<Book> getAll() {
retrun bookService.getall();
}
}