SSM整合就是将Spring,SpringMVC,MyBatis整合在一起,是标准的MVC模式,将整个系统划分为controller(表现层),service(业务逻辑层),dao(数据层)
使用spring MVC负责请求的转发和视图管理
spring实现业务对象管理,mybatis作为数据对象的持久化引擎
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VT5jtBNp-1686152735261)(D:\CSDN\img\maven.png)]
<dependencies>
<!-- spring集成mvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<!-- spring集成jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<!-- spring集成测试 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<!-- 注意:Spring集成的这几个坐标的版本要一致,否则会出错 -->
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!-- mybatis整合spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<!-- druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- json -->
<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.2</version>
<configuration>
<port>80</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YgGR4gA4-1686152735261)(D:\CSDN\img\tomcat.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fCRl8QMg-1686152735262)(D:\CSDN\img\proj.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-s8b8aMLD-1686152735262)(D:\CSDN\img\spring.png)]
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db1
jdbc.username=root
jdbc.password=root
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;
/**
* 配置数据源
* @return
*/
@Bean
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
}
public class MyBatisConfig {
/**
* 注册并配置SqlSessionFactoryBean实例,并将之前配置好的DataSource实例注入进去
* @param dataSource
* @return
*/
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setTypeAliasesPackage("com.ssm.integration.pojo");// 设置别名
// 如果dao包下存在mapper映射的.xml文件,则需指定.xml文件的位置
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResource("classpath:com/**/dao/impl/*.xml"));
return factoryBean;
}
/**
* 扫描指定包下的bean,将dao层的接口自动代理生成实现类(在接口里需要声明注解式的SQL语句);可以不用加@Mapper注解
* @return
*/
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer msc = new MapperScannerConfigurer();
msc.setBasePackage("com.ssm.integration.dao");
return msc;
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OliChxGq-1686152735262)(D:\CSDN\img\serv.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-66A0ZaP9-1686152735263)(D:\CSDN\img\serv.png)]
public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{SpringConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setEncoding("UTF-8");
return new Filter[]{characterEncodingFilter};
}
}
@Repository
public class Book {
private Integer id;
private String bookname;
private String describe;
private String press;
private String type;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public String getDescribe() {
return describe;
}
public void setDescribe(String describe) {
this.describe = describe;
}
public String getPress() {
return press;
}
public void setPress(String press) {
this.press = press;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "BookInfo{" +
"id=" + id +
", bookname='" + bookname + '\'' +
", describe='" + describe + '\'' +
", press='" + press + '\'' +
", type='" + type + '\'' +
'}';
}
}
@Repository
public interface BookInfoMapper {
@Select("select * from tbl_bookinfo")
List<BookInfo> selectAll();
@Select("select * from tbl_bookinfo where bookname=#{bookname}")
BookInfo selectBookName(String bookname);
@Insert("insert into tbl_bookinfo values (null,#{bookname},#{describe},#{press},#{type})")
void insertBook(BookInfo bookInfo);
@Delete("delete from tbl_bookinfo where id=#{id}")
void delete(Integer id);
@Update("update tbl_bookinfo set bookname=#{bookname},`describe`=#{describe},press=#{press},type=#{type} where id=#{id}")
void update(BookInfo bookInfo);
}
public interface BookService {
/**
* 根据id删除
* @param id
* @return
*/
boolean delete(Integer id);
/**
* 修改数据
* @param bookInfo
* @return
*/
boolean update(BookInfo bookInfo);
/**
* 插入数据
* @param bookInfo
* @return
*/
boolean insertBook(BookInfo bookInfo);
/**
* 根据id查询
* @param bookname
* @return
*/
BookInfo selectBookName(String bookname);
/**
* 查询全部
* @return
*/
List<BookInfo> selectAll();
}
@Service
public class BookInfoServiceImpl implements BookServiceImpl {
@Autowired
private BookInfoMapper bookInfoMapper;
@Override
public boolean delete(Integer id) {
bookInfoMapper.delete(id);
return true;
}
@Override
public boolean update(BookInfo bookInfo) {
bookInfoMapper.update(bookInfo);
return true;
}
@Override
public List<BookInfo> selectAll() {
return bookInfoMapper.selectAll();
}
@Override
public boolean insertBook(BookInfo bookInfo) {
bookInfoMapper.insertBook(bookInfo);
return true;
}
@Override
public BookInfo selectBookName(String bookname) {
return bookInfoMapper.selectBookName(bookname);
}
}
.update(bookInfo);
return true;
}
@Override
public List<BookInfo> selectAll() {
return bookInfoMapper.selectAll();
}
@Override
public boolean insertBook(BookInfo bookInfo) {
bookInfoMapper.insertBook(bookInfo);
return true;
}
@Override
public BookInfo selectBookName(String bookname) {
return bookInfoMapper.selectBookName(bookname);
}
}