如何使用use Spring MVC
和JdbcTemplate
类在Employee
表上执行CRUD操作?
有什么建议吗?
答案 0 :(得分:2)
有点难以解释,但我会尽我所能。
首先创建一个模型类说
public class Employee {
private int id;
private String name;
private String email;
private String address;
private String telephone;
public Employee() {
}
public Employee(int id,String name, String email, String address, String telephone) {
this.id=id;
this.name = name;
this.email = email;
this.address = address;
this.telephone = telephone;
}
// add getters and setters here
}
定义数据访问接口(DAO)
public interface EmployeeDAO {
public void saveOrUpdate(Employee employee);
public void delete(int Id);
public Employee get(int Id);
public List<Employee> list();
}
现在实施DAO
public class EmployeeDAOImpl implements EmployeeDAO {
private JdbcTemplate jdbcTemplate;
public EmployeeDAOImpl(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);
}
@Override
public void saveOrUpdate(Employee employee) {
// implementation details goes here...
}
@Override
public void delete(int id) {
// implementation details goes here...
}
@Override
public List<Employee> list() {
// implementation details goes here...
}
@Override
public Contact get(int Id) {
// implementation details goes here...
}
}
如果我确切知道你所尝试的内容以及你被击中的地方,我可以帮助你!当我不知道你到底在哪里时很难解释如何去做。
答案 1 :(得分:0)
如果你需要一个骨架来开始使用Spring MVC CRUD
答案 2 :(得分:0)
请先配置应用程序上下文。
然后您可以使用JdbcTemplate
。
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://${mysql.host}:${mysql.port}/${mysql.db}?characterEncoding=utf8&autoReconnectForPools=true</value>
</property>
<property name="username">
<value>${mysql.username}</value>
</property>
<property name="password">
<value>${mysql.password}</value>
</property>
</bean>
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>