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>
    

      

  • 相关阅读:
    Dubbo使用
    JVM内存分配及GC简述
    深入理解ThreadLocal
    Java的Timer定时器
    https与http的区别
    SpringBoot微服务
    Java的BIO,NIO,AIO
    Java常量池
    Java中的值传递与引用传递
    面向对象三大特征及代码优化七大原则
  • 原文地址:https://www.cnblogs.com/xu06123/p/8595658.html
Copyright © 2011-2022 走看看