SpringMVC入门第三部分,java面试具体场景的解决方案

taglibs

standard

1.1.2

2.web.xml进行配置


Tips: 字符编码的Filter配置要在支持REST风格的filter配置之前

DispatcherServlet

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:SpringMVC.xml

1

DispatcherServlet

/

CharacterEncodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

forceEncoding

true

CharacterEncodingFilter

/*

HiddenHttpMethodFilter

org.springframework.web.filter.HiddenHttpMethodFilter

HiddenHttpMethodFilter

/*

3.SpringMVC.xml配置


<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns=“http://www.springframework.org/schema/beans”

xmlns:context=“http://www.springframework.org/schema/context”

xmlns:mvc=“http://www.springframework.org/schema/mvc”

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<mvc:annotation-driven />

<context:component-scan base-package=“com”/>


数据库环境搭建

======================================================================

在这里插入图片描述

1.propertise配置文件


jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/tx

jdbc.username=root

jdbc.password=xxxxxxx

2.在Spring容器中引入pro配置文件,配置数据源,配置JDBCTemplate对象


SpringMVC.xml

<context:property-placeholder location=“classpath:jdbc.properties”/>


Dao层

===================================================================

EmployeeDao类:

@Repository

public class EmployeeDao {

@Autowired //按照类型注入

JdbcTemplate jdbcTemplate;

//保存员工信息到数据库

public void save( Employee employee)

{

String sql=“insert employee values(?,?,?,?,?)”;

jdbcTemplate.update(sql,employee.getId(),employee.getName(),employee.getDepartment(),

employee.getEamil(),employee.getGender());

}

//查询并返回对应员工信息

public Employee exist(Employee employee)

{

String sql=“select * from employee where name=? and id= ?”;

try{

Employee employee1 = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper(Employee.class), employee.getName(), employee.getId());

return employee1;

}

catch (Exception e)

{

return null;

}

}

//删除某个员工的信息

public void delete(Employee employee)

{

String sql=“delete from employee where id=?”;

jdbcTemplate.update(sql,employee.getId());

}

//返回所有员工的信息

public List getAllEmployees()

{

String sql=“select* from employee”;

List list = jdbcTemplate.query(sql, new BeanPropertyRowMapper(Employee.class));

return list;

}

}


POJO—自定义对象层

==========================================================================

employee类:

@Component

public class Employee {

private Integer id;

private String name;

private String department;

private String eamil;

private Integer gender;

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 String getDepartment() {

return department;

}

public void setDepartment(String department) {

this.department = department;

}

public String getEamil() {

return eamil;

}

public void setEamil(String eamil) {

this.eamil = eamil;

}

public Integer getGender() {

return gender;

}

public void setGender(Integer gender) {

this.gender = gender;

}

@Override

public String toString() {

return “Employee{” +

“id=” + id +

“, name=’” + name + ‘’’ +

“, department=’” + department + ‘’’ +

“, eamil=’” + eamil + ‘’’ +

“, gender=” 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值