SpringMVC(一)コメント

11914 ワード

1.pojoバッグ
2.mapperバッグ
(注意:ここではIMP実装クラスは不要です.)(mapperとmapper.xml)が含まれています.
3.サービスバッグ
(注意:@TransactionalはserviceIMPにあります)
@Service
@Transactional
//                    spring  
//           ,     spring      ,      
public class AccountServiceIMP implements AccountService {

    @Resource
    private AccountMapper accountMapper;

    @Override
    public List queryAccount() {
        return accountMapper.findAll();
    }
4.WEB-INFの下にスプリングMVC-servlet.xmlを建設する.
(注意:ここは[servlet-name]-servlet.xmlです.web.xmlの中のservlet-nameと一致します.)

<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"mvcxmlns: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 

    <mvc:annotation-driven/>

    
    <context:component-scan base-package="com"/>

    
    <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>

    
    
    <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    bean>

    
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations">
            <value>classpath:com/mapper/*.xmlvalue>
        property>
    bean>

    
    

    
    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.mapper"/>
    bean>
5.web.xml中
   <servlet>
        <servlet-name>springMVCservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>

    servlet>
    <servlet-mapping>
        <servlet-name>springMVCservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>
6.Controller
1)@Controller注解このclassをcontroller 2に指定します.@Request Mapping注解のvalue値はこのcontrollerにマッピングされた要求を指定します.3)Findメソッドパラメータは、HttpServrequest request.findなのでデータを携帯する必要があります.修正後はデータを携帯する必要がないので、find方法を再起動する必要があります.リダイレクトレスポンス.sendredict()->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 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");
    }

}