springmvc小项目

package com.zxp.domain;
import java.util.Date;
/**
 * @author zxp
 */
public class Items {

    private Integer id;
    private String name;
    private Double price;
    private String pic;
    private Date createtime;
    private String detail;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public String getPic() {
        return pic;
    }

    public void setPic(String pic) {
        this.pic = pic;
    }

    public Date getCreatetime() {
        return createtime;
    }

    public void setCreatetime(Date createtime) {
        this.createtime = createtime;
    }

    public String getDetail() {
        return detail;
    }

    public void setDetail(String detail) {
        this.detail = detail;
    }
}

package com.zxp.dao;

import com.zxp.domain.Items;

/**

  • @author zxp
    */
    public interface ItemsDao {

    public Items findById(Integer id);
    }

package com.zxp.service;

import com.zxp.domain.Items;

/**
 * @author zxp
 */
public interface ItemsServiceDao {
    public Items findById(Integer id);
}

```package com.zxp.service.impl;

import com.zxp.dao.ItemsDao;
import com.zxp.domain.Items;
import com.zxp.service.ItemsServiceDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zxp
 */
@Service
public class ItemsServiceImpl implements ItemsServiceDao {

    @Autowired
    private ItemsDao itemsDao;
    public Items findById(Integer id) {
        return itemsDao.findById(id);
    }
}
<?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="com.zxp.dao.ItemsDao">
    <select id="findById" parameterType="int" resultType="items">
        select * from items where id =#{id}
    </select>
</mapper>
package com.zxp.controller;

import com.zxp.domain.Items;
import com.zxp.service.ItemsServiceDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author zxp
 */
@Controller
@RequestMapping("/items")
public class ItemsController {

    @Autowired
    private ItemsServiceDao itemsServiceDao;

    @RequestMapping("/findDetials")
    public String findDetials(Model model){

        Items byId = itemsServiceDao.findById(1);
       model.addAttribute("item",byId);
        return "itemDetail";

    }
}
<?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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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
			    http://www.springframework.org/schema/context
			    http://www.springframework.org/schema/context/spring-context.xsd
			    http://www.springframework.org/schema/aop
			    http://www.springframework.org/schema/aop/spring-aop.xsd
			    http://www.springframework.org/schema/tx
			    http://www.springframework.org/schema/tx/spring-tx.xsd
			    http://www.springframework.org/schema/mvc
			    http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--dao配置文件开始-->

    <!--配置连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/maven?serverTimezone=UTC"></property>
        <property name="username" value="root"></property>
        <property name="password" value="zp1536338"></property>
    </bean>

    <!--Sqlsession-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
     <!--扫描pojo包,给包下所有pojo对象起别名-->
        <property name="typeAliasesPackage" value="com.zxp.domain"></property>


    </bean>
    <!--扫描接口包路径,生成包所有接口的代理对象,放入容器中-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.zxp.dao"></property>
    </bean>
    <!--dao配置文件结束-->

<!--配置组件扫描配置-->
<context:component-scan base-package="com.zxp.service"/>

    <!--配置事务管理器-->
    <bean id="transactionManager" class="  org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>

    </bean>
    <!--配置事务通知-->
    <tx:advice id="advice">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="find*" read-only="true" />
            <tx:method name="*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>


    <!--配置切面-->
    <aop:config>
        <aop:pointcut id="poincut" expression= "execution(* com.zxp.service.impl.*.*(..))"></aop:pointcut>
        <aop:advisor advice-ref="advice" pointcut-ref="poincut"/>
    </aop:config>
 </beans>
 # Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE            debug   info   warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE

# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=d:\axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

<?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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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
			    http://www.springframework.org/schema/context
			    http://www.springframework.org/schema/context/spring-context.xsd
			    http://www.springframework.org/schema/aop
			    http://www.springframework.org/schema/aop/spring-aop.xsd
			    http://www.springframework.org/schema/tx
			    http://www.springframework.org/schema/tx/spring-tx.xsd
			    http://www.springframework.org/schema/mvc
			    http://www.springframework.org/schema/mvc/spring-mvc.xsd">

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

    <mvc:annotation-driven />

    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp" />

    </bean>
<mvc:default-servlet-handler />
</beans>
package com.zxp.test;


import com.zxp.dao.ItemsDao;
import com.zxp.domain.Items;
import com.zxp.service.ItemsServiceDao;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author zxp
 */
public class ItemTest {


   @Test
   public void findById(){
      ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

//      ItemsDao itemsDao = ac.getBean(ItemsDao.class);

      Items byId = itemsDao.findById(1);

      System.out.println(byId.getName());

      ItemsServiceDao bean = ac.getBean(ItemsServiceDao.class);
      Items byId = bean.findById(1);
      System.out.println(byId.getName());
   }
}

©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页