zoukankan      html  css  js  c++  java
  • SSM

    1.导入jar包

          

            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.2.2</version>
            </dependency>
    
            <!--Mybatis+Spring整合-->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>1.2.1</version>
            </dependency>
    
            <!-- Spring整合JavaWeb的包 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>4.2.0.RELEASE</version>
            </dependency>
    

    2.创建实体类

    public class Book {
     private String bookauthor;
        private String bookname;
        private Integer bookprice;
    
        public String getBookauthor() {
            return bookauthor;
        }
    
        public void setBookauthor(String bookauthor) {
            this.bookauthor = bookauthor;
        }
    
        public String getBookname() {
            return bookname;
        }
    
        public void setBookname(String bookname) {
            this.bookname = bookname;
        }
    
        public Integer getBookprice() {
            return bookprice;
        }
    
        public void setBookprice(Integer bookprice) {
            this.bookprice = bookprice;
        }
    }
    

      3.接口和小配置

    public interface IBookDAO {
        //添加图书
        public int addBook(Book book);
    }
    

      

    <?xml version="1.0" encoding="UTF-8" ?>
        <!DOCTYPE mapper
                PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
                "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
        <mapper namespace="dayssm.dao.IBookDAO">
         <!--添加图书-->
        <insert id="addBook">
            insert into book(bookname,bookauthor,bookprice) values(#{bookname},#{bookauthorw},#{bookprice})
        </insert>
    
        </mapper>
    

      4.service层:

    public interface IBookService {
        //添加图书
        public int addBook(Book book);
    }
    

      

    public class BookServiceImpl implements IBookService {
        //植入dao
        private IBookDAO bookDAO;
        public int addBook(Book book) {
            return bookDAO.addBook(book);
        }
    
        public IBookDAO getBookDAO() {
            return bookDAO;
        }
    
        public void setBookDAO(IBookDAO bookDAO) {
            this.bookDAO = bookDAO;
        }
    }
    

      5.servlet层

    public class BookServlet extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.setCharacterEncoding("utf-8");
            String bookname=request.getParameter("bookname");
            String bookauthor=request.getParameter("bookauthor");
            int bookprice=Integer.parseInt(request.getParameter("bookprice"));
    
            Book book=new Book();
            book.setBookname(bookname);
            book.setBookprice(bookprice);
            book.setBookauthor(bookauthor);
           //ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext23ssm.xml");
            ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
            IBookService service=(IBookService) ac.getBean("service");
            int i=service.addBook(book);
            if (i>0){
                request.getRequestDispatcher("/index.jsp").forward(request,response);
            }else{
                request.getRequestDispatcher("/add.jsp").forward(request,response);
            }
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doPost(request,response);
        }
    }
    

      6.配置

      <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
            <property name="url" value="jdbc:mysql://localhost:3306/smbms"></property>
            <property name="username" value="root"></property>
            <property name="password" value=""></property>
        </bean>
    
        <!--2.识别到jdbc.properties文件-->
        <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="location" value="classpath:jdbc.properties"></property>
        </bean>
    
        <!--关键点:SqlSessionFactory生成权交给Spring-->
        <bean class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"></property>
            <!--大配置-->
            <property name="configLocation" value="classpath:MyBatis-Config.xml"></property>
            <!--<property name="mapperLocations" value="classpath:mapper/*.xml"></property>-->
        </bean>
    
        <!--3.bookDAO    没有实现类-->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="dayssm.dao"></property>
        </bean>
    
    
        <!--5.service id-->
        <bean id="service" class="dayssm.service.BookServiceImpl">
            <property name="bookDAO" ref="IBookDAO"></property>
        </bean>
    
        <!--事务:事务管理器-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
    
        <!--方式三:AspectJ AOP 注解-->
        <tx:advice id="stockAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="buy*" isolation="DEFAULT" propagation="REQUIRED" rollback-for="StockException"/>
                <tx:method name="select*" isolation="DEFAULT" propagation="REQUIRED" read-only="true"/>
            </tx:attributes>
        </tx:advice>
    
        <aop:config>
            <!--1.切点-->
            <aop:pointcut id="mypointcut" expression="execution(* *..service.*.*(..))"></aop:pointcut>
            <!--3.顾问-->
            <aop:advisor advice-ref="stockAdvice" pointcut-ref="mypointcut"></aop:advisor>
            <!--2.切面-->
    
    
        </aop:config>
    

      

    <?xml version="1.0" encoding="UTF-8" ?>
            <!DOCTYPE configuration
                    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
                    "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
       <typeAliases>
           <package name="dayssm.dao"></package>
           <package name="dayssm.entity"></package>
       </typeAliases>
    <mappers>
        <mapper resource="dayssm/dao/IBookDAO.xml"/>
    </mappers>
    </configuration>
    

      7.jsp页面

                   

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <h1>添加图书</h1>
    <form action="/Bookservlet" method="post">
        图书名称:<input name="bookname"/>
        图书作者:<input name="bookauthor"/>
        图书价格:<input name="bookprice"/>
        <input type="submit" value="添加"/>
    </form>
    </body>
    </html>
    

      

  • 相关阅读:
    数据库课程设计心得【3】存储过程的运用
    看BBC研究大脑的科教片中“放松产生灵感”的笔记
    成功干掉“恶心的U盘自动运行病毒免疫目录”!共享方法,让更多的人干掉这东西!
    分享一大堆最新dot net asp.net c# 电子书下载 , 英文原版的。经典中的经典
    SQL学习之 对GROUP BY 和 HAVING 的理解 学习笔记
    关于Theme中.skin与css需要理清的关系
    最近的学习笔记,记录一些通俗易懂的学习类文章。更像是好资料参与索引。
    关于DNN Module开发学习以来的一点总结
    工具发布!QQ空间阅读与备份工具
    被忽视的大道理
  • 原文地址:https://www.cnblogs.com/xu06123/p/8595658.html
Copyright © 2011-2022 走看看