栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

maven中操作数据库实现省市联动最后拓展到redis(1)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

maven中操作数据库实现省市联动最后拓展到redis(1)

一:准备工作

数据库,在城市表中省份id作为外键

pom. xml文件

 
      javax.servlet
      javax.servlet-api
      3.0.1
    
    
      javax.servlet.jsp
      jsp-api
      2.2
    
    
      mysql
      mysql-connector-java
      5.0.5
    
    
      jstl
      jstl
      1.2
    
    
    
      commons-beanutils
      commons-beanutils
      1.9.3
    
    
      commons-collections
      commons-collections
      3.2.1
    
    
      commons-lang
      commons-lang
      2.6
    
    
      commons-logging
      commons-logging
      1.1.1
    
    
      net.sf.ezmorph
      ezmorph
      1.0.6
    
    
      net.sf.json-lib
      json-lib
      2.4
      jdk13
    

maven工程结构

 index.jsp文件

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>


    
    


    
        
    
    


接下来我们完成dao层,service层,action,util,jsp

ProvinceCityDao

public interface ProvinceCityDao {
    
    public List getProvince();
    
    public List  getCity(String codePid);//省份的id
}

ProvinceCityService 

public interface ProvinceCityService {
    
    public List getProvince();
    
    public List  getCity(String codePid);//省份的id
}

ProvinceCityServiceImpl 

public class ProvinceCityServiceImpl implements ProvinceCityService{
    ProvinceCityDao provinceCityDao = new ProvinceDaoImpl();
    //查询省
    public List getProvince() {
        return provinceCityDao.getProvince();
    }

    //查询市
    public List getCity(String codePid) {
        return provinceCityDao.getCity(codePid);
    }
}

 ProvinceAction

@WebServlet("/getProvince")
public class ProvinceAction extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //处理请求编码格式 Request
        req.setCharacterEncoding("UTF-8");
        //处理  Response 响映json编码格式
        resp.setContentType("application/json;charset=UTF-8");

        ProvinceCityService provinceCityService = new ProvinceCityServiceImpl();
        List list = provinceCityService.getProvince();

        //citys转换成jsonArray格式
        JSONArray jsonArray = JSONArray.fromObject(list);
        resp.getWriter().println(jsonArray);
    }
}

CityAction

@WebServlet("/getCity")
public class CityAction extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //处理请求编码格式 Request
        req.setCharacterEncoding("UTF-8");
        //处理  Response 响映json编码格式'
        resp.setContentType("application/json;charset=UTF-8");
        //获取参数
        String codePid = req.getParameter("id");
        //创建控制层
        ProvinceCityService provinceCityService = new ProvinceCityServiceImpl();
        List list = provinceCityService.getCity(codePid);

        //citys转换成jsonArray格式
        JSONArray jsonArray = JSONArray.fromObject(list);
        resp.getWriter().println(jsonArray);
    }
}

entity层

public class Province {
    private  String id;
    private  String codePid;
    private  String name;
public class City {
    private  String id;
    private  String codeCid;
    private  String name;
    private  String codePid;

util

public class BaseDao {
    private static String driveName;
    private static String url;
    private static String username;
    private static String password;
    static {
        init();
    }

    public static void init() {
        //获取配置文件
        InputStream inputStream = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
        Properties properties = new Properties();
        try {
            properties.load(inputStream);
            driveName = properties.getProperty("db.driverName");
            url = properties.getProperty("db.url");
            username = properties.getProperty("db.userName");
            password = properties.getProperty("db.pwd");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    
    public static Connection getConnection(){
        Connection conn = null;
        try {
            Class.forName(driveName);
            conn = DriverManager.getConnection(url,username,password);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    
    public static void closeAll(ResultSet rs, Statement sta, Connection conn){
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (sta != null) {
            try {
                sta.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

 配置文件

db.driverName=com.mysql.jdbc.Driver
db.url=jdbc:mysql://127.0.0.1:3306/mysql02?useUnicode=true&characterEncoding=utf-8
db.userName=root
db.pwd=123456

最关键的index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>


    
    


    
    

最后效果

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/937719.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号