1. SSM整合配置
SM整合流程
创建工程 SSM整合
Spring
MyBatis
MybatisConfig JdbcConfig jdbc.properties SpringMVC
ServletContainerInitConfig SpringMvcConfig
1.1 创建工程,添加依赖和插件
< dependencies>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-webmvc</ artifactId>
< version> 5.2.10.RELEASE</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-jdbc</ artifactId>
< version> 5.2.10.RELEASE</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-test</ artifactId>
< version> 5.2.10.RELEASE</ version>
</ dependency>
< dependency>
< groupId> org.mybatis</ groupId>
< artifactId> mybatis</ artifactId>
< version> 3.5.6</ version>
</ dependency>
< dependency>
< groupId> org.mybatis</ groupId>
< artifactId> mybatis-spring</ artifactId>
< version> 1.3.0</ version>
</ dependency>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
< version> 5.1.47</ version>
</ dependency>
< dependency>
< groupId> com.alibaba</ groupId>
< artifactId> druid</ artifactId>
< version> 1.1.16</ version>
</ dependency>
< dependency>
< groupId> junit</ groupId>
< artifactId> junit</ artifactId>
< version> 4.12</ version>
< scope> test</ scope>
</ dependency>
< dependency>
< groupId> javax.servlet</ groupId>
< artifactId> javax.servlet-api</ artifactId>
< version> 3.1.0</ version>
< scope> provided</ scope>
</ dependency>
< dependency>
< groupId> com.fasterxml.jackson.core</ groupId>
< artifactId> jackson-databind</ artifactId>
< version> 2.9.0</ version>
</ dependency>
</ dependencies>
< build>
< plugins>
< plugin>
< groupId> org.apache.tomcat.maven</ groupId>
< artifactId> tomcat7-maven-plugin</ artifactId>
< version> 2.1</ version>
< configuration>
< port> 80</ port>
< path> /</ path>
</ configuration>
</ plugin>
</ plugins>
</ build>
1.2 Spring整合Mybatis
1.2.1 jdbc.properties属性文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/ssm?SSL=false
jdbc.username=root
jdbc.password=1234
1.2.2 JdbcConfig配置类
package com. zhang. config ;
import com. alibaba. druid. pool. DruidDataSource ;
import org. springframework. beans. factory. annotation. Value ;
import org. springframework. context. annotation. Bean ;
import javax. sql. DataSource ;
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 getDataSource ( ) {
DruidDataSource dataSource = new DruidDataSource ( ) ;
dataSource. setDriverClassName ( driver) ;
dataSource. setUrl ( url) ;
dataSource. setUsername ( username) ;
dataSource. setPassword ( password) ;
return dataSource;
}
@Bean
public PlatformTransactionManager transactionManager ( DataSource dataSource) {
DataSourceTransactionManager ds = new DataSourceTransactionManager ( ) ;
ds. setDataSource ( dataSource) ;
return ds;
}
}
1.2.3 MybatisConfig配置类
package com. zhang. config ;
import org. mybatis. spring. SqlSessionFactoryBean ;
import org. mybatis. spring. mapper. MapperScannerConfigurer ;
import org. springframework. context. annotation. Bean ;
import javax. sql. DataSource ;
public class MybatisConfig {
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean ( DataSource dataSource) {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean ( ) ;
sqlSessionFactoryBean. setDataSource ( dataSource) ;
sqlSessionFactoryBean. setTypeAliasesPackage ( "com.zhang.domain" ) ;
return sqlSessionFactoryBean;
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer ( ) {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer ( ) ;
mapperScannerConfigurer. setBasePackage ( "com.zhang.dao" ) ;
return mapperScannerConfigurer;
}
}
1.2.4 SpringConfig配置类
package com. zhang. config ;
import org. springframework. context. annotation. ComponentScan ;
import org. springframework. context. annotation. Configuration ;
import org. springframework. context. annotation. Import ;
import org. springframework. context. annotation. PropertySource ;
import org. springframework. transaction. annotation. EnableTransactionManagement ;
@Configuration
@ComponentScan ( { "com.zhang.dao" , "com.zhang.service" } )
@PropertySource ( "classpath:jdbc.properties" )
@Import ( { JdbcConfig . class , MybatisConfig . class } )
@EnableTransactionManagement
public class SpringConfig {
}
1.3 Spring整合SpringMVC
1.3.1 SpringMvcConfig配置类
package com. zhang. config ;
import org. springframework. context. annotation. ComponentScan ;
import org. springframework. context. annotation. Configuration ;
import org. springframework. web. servlet. config. annotation. EnableWebMvc ;
@Configuration
@ComponentScan ( "com.zhang.controller" )
@EnableWebMvc
public class SpringMvcConfig {
}
1.3.2 ServletContainerInitConfig配置类,加载SpringMvcConfig和SpringConfig配置类
package com. zhang. config ;
import org. springframework. web. context. WebApplicationContext ;
import org. springframework. web. context. support. AnnotationConfigWebApplicationContext ;
import org. springframework. web. servlet. support. AbstractDispatcherServletInitializer ;
public class ServletContainerInitConfig extends AbstractDispatcherServletInitializer {
protected WebApplicationContext createServletApplicationContext ( ) {
AnnotationConfigWebApplicationContext webApplicationContext = new AnnotationConfigWebApplicationContext ( ) ;
webApplicationContext. register ( SpringMvcConfig . class ) ;
return webApplicationContext;
}
protected String [ ] getServletMappings ( ) {
return new String [ ] { "/" } ;
}
protected WebApplicationContext createRootApplicationContext ( ) {
AnnotationConfigWebApplicationContext webApplicationContext = new AnnotationConfigWebApplicationContext ( ) ;
webApplicationContext. register ( SpringConfig . class ) ;
return webApplicationContext;
}
}
2. 功能模块开发
2.1 数据层
package com. zhang. dao ;
import com. zhang. domain. Book ;
import org. apache. ibatis. annotations. Delete ;
import org. apache. ibatis. annotations. Insert ;
import org. apache. ibatis. annotations. Select ;
import org. apache. ibatis. annotations. Update ;
import java. util. List ;
public interface BookDao {
@Insert ( "insert into tbl_book values(null,#{type},#{name},#{description}))" )
void save ( Book book) ;
@Delete ( "delete from tbl_book where id =#{id}}" )
void deleteById ( 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 queryById ( Integer id) ;
}
2.2 业务层接口
package com. zhang. service ;
import com. zhang. domain. Book ;
import java. util. List ;
public interface BookService {
Boolean save ( Book book) ;
Boolean deleteById ( Integer id) ;
Boolean update ( Book book) ;
Book queryById ( Integer id) ;
List < Book > getAll ( ) ;
}
2.3 业务层实现类
package com. zhang. service. impl ;
import com. zhang. dao. BookDao ;
import com. zhang. domain. Book ;
import com. zhang. service. BookService ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. stereotype. Service ;
import java. util. List ;
@Service
@Transactional
public class BookServiceImpl implements BookService {
@Autowired
private BookDao bookDao;
public Boolean save ( Book book) {
return true ;
}
public Boolean deleteById ( Integer id) {
return true ;
}
public Boolean update ( Book book) {
return true ;
}
public Book queryById ( Integer id) {
return bookDao. queryById ( id) ;
}
public List < Book > getAll ( ) {
List < Book > bookList = bookDao. getAll ( ) ;
return bookList;
}
}
2.4 Controller
package com. zhang. controller ;
import com. zhang. domain. Book ;
import com. zhang. service. BookService ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. web. bind. annotation. * ;
import java. util. List ;
@RestController
@RequestMapping ( "/books" )
public class BookController {
@Autowired
private BookService bookService;
@PostMapping ( "/save" )
public Boolean save ( @RequestBody Book book) {
return bookService. save ( book) ;
}
@DeleteMapping ( "/delete/{id}" )
public Boolean deleteById ( @PathVariable ( "id" ) Integer id) {
return bookService. deleteById ( id) ;
}
@PutMapping ( "/update" )
public Boolean update ( @RequestBody Book book) {
return bookService. update ( book) ;
}
@GetMapping ( "getAll/{id}" )
public Book queryById ( @PathVariable ( "id" ) Integer id) {
Book book = bookService. queryById ( id) ;
return book;
}
@GetMapping ( "/getAll" )
public List < Book > getAll ( ) {
List < Book > books = bookService. getAll ( ) ;
System . out. println ( "查询成功" ) ;
return books;
}
}