1.pojo包
2.mapper包
( 注意:这里不需要 IMP 实现类)
包含(mapper和 mapper.xml)
3.Service包
(注意:@Transactional在 serviceIMP 中)
实现类:
@Service
@Transactional
// 该类里面的所有方法或者这个方法的事务由spring处理
// 方法里面对数据库操作,如果失败则spring负责回滚操作,成功提交操作
public class AccountServiceIMP implements AccountService {
@Resource
private AccountMapper accountMapper;
@Override
public List<Account> queryAccount() {
return accountMapper.findAll();
}
4.在 WEB-INF 下建springMVC-servlet.xml
(注意:注意这里是[servlet-name]-servlet.xml,要和web.xml 里面的servlet-name一致)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
【手打mvc 可以跳出来】 xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
【增加context:】
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
【增加mvc:】
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
<!-- 1.mvc注解驱动 -->
<mvc:annotation-driven/>
<!--2.全局扫描包 -->
<context:component-scan base-package="com"/>
<!-- 3.驱动管理数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/student"/>
<property name="username" value="root"/>
<property name="password" value="123"/>
</bean>
<!-- 4.数据源事务管理 -->
<!-- 开 启 Spring 的 事 务 处 理-->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 5.sqlsessionfatorybean -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations">
<value>classpath:com/mapper/*.xml</value>
</property>
</bean>
<!-- 6.spring和mybaits整合:映射输入参数 -->
<!--MapperFactoryBean作用:注入sqlSessionFactoryBean产出 mapper 实现类-->
<!--当发现要使用多个MapperFactoryBean,MapperScannerConfigurer类会查找类路径下的映射器并自动将它们创建成MapperFactoryBean。-->
<!--扫描org.mybatis.spring.sample.mapper下的所有接口,然后创建各自接口的动态代理类-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mapper"/>
</bean>
5.web.xml 中
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
6.Controller
1)@Controller 注解把该class指定为controller
2)@RequestMapping 注解的 value值指定该controller所映射的请求
3)Find方法参数有:HttpServletRequest request,因为是 find 需要携带数据—>return “/index.jsp”—>方法返回值String
4 )update 等方法参数: HttpServletResponse response,因为修改后就不需要携带数据了,且需要重调用 find方法—>重定向 response.sendredirect()—>void
5)注意404错误
@Controller
/* @Controller 注解把该class指定为controller */
@RequestMapping("/account")
public class AccountController {
@Resource
private AccountServiceIMP accountServiceImpl;
/* @RequestMapping 注解的 value值指定该controller所映射的请求 */
@RequestMapping("/findAll.do")
public String findAll(HttpServletRequest request){
List<Account> list=accountServiceImpl.queryAccount();
request.setAttribute("list", list);
return "/index.jsp";
// 1. 转发出去,直接到 index.jsp
}
@RequestMapping("/findbyid.do")
public String findById(HttpServletRequest request,Integer id){
Account account=accountServiceImpl.findById(id);
request.setAttribute("account", account);
return "/update.jsp";
}
@RequestMapping("/update.do")
public void editAccount(HttpServletResponse response,Account account) throws IOException{
Integer num=accountServiceImpl.editAccount(account);
response.sendRedirect("findAll.do");
// 2. 重定向到/account/findAll.do
}
@RequestMapping("/delbyid.do")
public void delbyid(HttpServletResponse response,Integer id) throws IOException{
Integer num = accountServiceImpl.delByid(id);
response.sendRedirect("findAll.do");
}
@RequestMapping("/add.do")
public void add(HttpServletResponse response,Account account) throws IOException{
Integer num=accountServiceImpl.add(account);
response.sendRedirect("findAll.do");
}
}