SSM整合
步骤:
1.创建web的maven工程
2.在 pom.xml 中添加如下依赖:
<dependencies>
<!--引入mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!--引入spring-webmvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<!--引入spring-jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<!--spring和mybatis整合的依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<!--mysql驱动-->
<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.21</version>
</dependency>
<!--逆向工程-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.4.0</version>
</dependency>
<!--引入jaskson jar-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.11.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.11.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.4</version>
</dependency>
</dependencies>
3.在resource下创建springmvc.xml and spring.xml配置文件
例:
3.1 springmvc.xml配置文件
<!--开启扫描-->
<context:component-scan base-package="com.ganin.controller"/>
<!--开启注解驱动-->
<mvc:annotation-driven/>
<!--开启静态资源放行-->
<mvc:default-servlet-handler/>
<!--开启视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="WEB-INF/views"/>
<property name="suffix" value=".jsp"/>
</bean>
3.2spring.xml配置文件(这里我起名字为applicationContext.xml)
<!--开启包扫描-->
<context:component-scan base-package="com.ganin.service"/>
<!--配置数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="username" value="root"/>
<property name="password" value="654123"/>
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
</bean>
<!--SqlSessionFactory-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" >
<property name="dataSource" ref="dataSource"/>
<!--加载mybatis的配置文件-->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!--为Dao层接口生成实现类对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ganin.dao"/>
</bean>
4.配置web.xml
<!--前端控制器dispatcherServlet-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--指定加载的配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!--启动Tomcat时加载配置文件-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--指定加载的配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
5.通过逆向工程 生成dao、eneity(实体)、mapper(参考小编上篇文章 mybatis逆向工程)
6.简单编写Service层和Controller层
代码如下
service层:
public interface AccountService {
/**
* 根据id查询相应数据
* @param id
* @return
*/
public Account selById(Integer id);
}
}
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountMapper accountMapper;
/**
* 根据id查询相应数据
* @param id
* @return
*/
@Override
public Account selById(Integer id) {
return accountMapper.selectByPrimaryKey(id);
}
}
controler层:
@RestController
public class AccountController {
@Autowired
private AccountService accountService;
/**
* 根据id查询相应数据
* @param id
* @return
*/
@RequestMapping("/selById")
public Account selById(Integer id){
return accountService.selById(id);
}
}
7.配置Tomcat并启动
看结果