零配置处理web请求的范例

package edu.xalead;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration
@Import(WebConfig.class)
@EnableTransactionManagement
public class RootConfig {

    @Bean
    public DataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/cms");
        dataSource.setUsername("root");
        dataSource.setPassword("root");

        return dataSource;
    }
    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource){
        return new JdbcTemplate(dataSource);
    }
    @Bean
    public DataSourceTransactionManager transactionManager(DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }
}

package edu.xalead;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@ComponentScan(basePackages = "edu.xalead.springmvc")
@EnableWebMvc //代替<mvc:annotation-driven />配置
public class WebConfig implements WebMvcConfigurer {
    // 配置jsp视图解析器
    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
}
package edu.xalead;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer {
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{RootConfig.class};
    }

    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{WebConfig.class};
    }

    protected String[] getServletMappings() {
        return new String[]{"*.action"};
    }
}
​
package edu.xalead.springmvc.dao;

import edu.xalead.springmvc.entity.Channel;

public interface UserDao {
    public void addChannel(Channel c);
}

​
package edu.xalead.springmvc.dao;

import edu.xalead.springmvc.entity.Channel;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
@Repository
public class UserDaoImpl implements UserDao{
    @Resource
    private JdbcTemplate jdbcTemplate;

    public void addChannel(Channel c) {
        String sql = "insert into t_channel values(?,?,?)";
        jdbcTemplate.update(sql,c.getCid(),c.getCname(),c.getDescription());
    }
}
package edu.xalead.springmvc.entity;

import lombok.Data;

@Data
public class Channel {
    private int cid;
    private String cname;
    private String description;
}
package edu.xalead.springmvc.service;

import edu.xalead.springmvc.entity.Channel;

public interface UserService {
    public void addUser(Channel c);
}
package edu.xalead.springmvc.service;

import edu.xalead.springmvc.dao.UserDao;
import edu.xalead.springmvc.entity.Channel;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;
    @Transactional
    public void addUser(Channel c) {
        userDao.addChannel(c);
    }
}
package edu.xalead.springmvc.web;

import edu.xalead.springmvc.entity.Channel;
import edu.xalead.springmvc.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

@Controller
public class UserController {
    @Resource
    private UserService userService;

    @RequestMapping("/addChannel")
    public String addChannel(Channel channel, Model m){
        userService.addUser(channel);
        m.addAttribute("c",channel);
        return "success";
    }

}
​
​
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/9/22
  Time: 16:51
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${c.cid}:${c.cname}:${c.description}<br/>
</body>
</html>

​

​

调试总结:

错误一

1、查看程序中路径是否写对。

2、查看maven的文件路径,本地库是否写对。

 错误二

添加信息的时候,数据库中id不能重复,因为id是主键。

发布了64 篇原创文章 · 获赞 47 · 访问量 9535

猜你喜欢

转载自blog.csdn.net/weixin_42194284/article/details/101207688