先展示项目结构
ClassBiz.java
package com.gxj.biz;
import java.util.List;
import com.gxj.model.Clazz;
public interface ClassBiz {
public boolean InsertClass(Clazz clazz);
public boolean DeleteClass(Integer id);
public boolean UpdateClass(Clazz clazz);
public List SelectClass();
}
ClassBizimplement.java
package com.gxj.biz;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.gxj.dao.ClassDao;
import com.gxj.model.Clazz;
@Service("classbiz")
public class ClassBizimplement implements ClassBiz {
@Resource
private ClassDao dao;
@Override
public boolean InsertClass(Clazz clazz) {
// TODO Auto-generated method stub
boolean flag = false;
int res = dao.InsertClass(clazz);
if(res>0) {
flag = true;
}
return flag;
}
@Override
public boolean DeleteClass(Integer id) {
// TODO Auto-generated method stub
boolean flag = false;
int res = dao.DeleteClass(id);
if(res>0) {
flag = true;
}
return flag;
}
@Override
public boolean UpdateClass(Clazz clazz) {
// TODO Auto-generated method stub
boolean flag = false;
int res = dao.UpdateClass(clazz);
if(res>0) {
flag = true;
}
return flag;
}
@Override
public List SelectClass() {
// TODO Auto-generated method stub
List list = dao.SelectClass();
return list;
}
}
ClassDao.java
package com.gxj.dao;
import java.util.List;
import com.gxj.model.Clazz;
public interface ClassDao {
public int InsertClass(Clazz clazz);
public int DeleteClass(Integer id);
public int UpdateClass(Clazz clazz);
public List SelectClass();
}
ClassDaoimplement.java
package com.gxj.dao;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import com.gxj.model.ClassRowMapper;
import com.gxj.model.Clazz;
@Repository
public class ClassDaoimplement implements ClassDao {
@Resource
private JdbcTemplate jt;
@Override
public int InsertClass(Clazz clazz) {
// TODO Auto-generated method stub
String sql = "insert into class(name,size) value(?,?)";
int res = jt.update(sql,new Object[] {clazz.getName(),clazz.getSize()});
return res;
}
@Override
public int DeleteClass(Integer id) {
// TODO Auto-generated method stub
String sql = "delete from class where id=?";
int res = jt.update(sql,new Object[] {id});
return res;
}
@Override
public int UpdateClass(Clazz clazz) {
// TODO Auto-generated method stub
String sql = "update class set name=?,size=? where id=?";
int res = jt.update(sql, new Object[] {clazz.getName(),clazz.getSize(),clazz.getId()});
return res;
}
@Override
public List SelectClass() {
// TODO Auto-generated method stub
String sql = "select * from class";
List list = jt.query(sql,new ClassRowMapper());
return list;
}
}
DeleteClass.java
package com.gxj.main;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.gxj.biz.ClassBiz;
public class DeleteClass {
public static void main(String[] args) {
// TODO Auto-generated method stub
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
ClassBiz biz = (ClassBiz) context.getBean("classbiz");
boolean flag = biz.DeleteClass(21);
System.out.println(flag);
}
}
InsertClass.java
package com.gxj.main;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.gxj.biz.ClassBiz;
import com.gxj.model.Clazz;
public class InsertClass {
public static void main(String[] args) {
// TODO Auto-generated method stub
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
ClassBiz biz = (ClassBiz) context.getBean("classbiz");
Clazz clazz = new Clazz();
clazz.setName("ts17061");
clazz.setSize(20);
boolean flag = biz.InsertClass(clazz);
System.out.println(flag);
}
}
SelectClass.java
package com.gxj.main;
import java.util.List;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.gxj.biz.ClassBiz;
import com.gxj.model.Clazz;
public class SelectClass {
public static void main(String[] args) {
// TODO Auto-generated method stub
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
ClassBiz biz = (ClassBiz) context.getBean("classbiz");
List list = biz.SelectClass();
for(Clazz p:list) {
System.out.println(p.getId());
System.out.println(p.getName());
System.out.println(p.getSize());
}
}
}
UpdateClass.java
package com.gxj.main;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.gxj.biz.ClassBiz;
import com.gxj.model.Clazz;
public class UpdateClass {
public static void main(String[] args) {
// TODO Auto-generated method stub
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
ClassBiz biz = (ClassBiz) context.getBean("classbiz");
Clazz clazz = new Clazz();
clazz.setId(20);
clazz.setName("ts17066");
clazz.setSize(55);
boolean flag = biz.UpdateClass(clazz);
System.out.println(flag);
}
}
ClassRowMapper.java
package com.gxj.model;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
public class ClassRowMapper implements RowMapper {
@Override
public Clazz mapRow(ResultSet arg0, int arg1) throws SQLException {
// TODO Auto-generated method stub
Clazz clazz = new Clazz();
clazz.setId(arg0.getInt(1));
clazz.setName(arg0.getString(2));
clazz.setSize(arg0.getInt(3));
return clazz;
}
}
Clazz.java
package com.gxj.model;
public class Clazz {
private Integer id;
private String name;
private Integer size;
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 Integer getSize() {
return size;
}
public void setSize(Integer size) {
this.size = size;
}
}
spring.xml
root //数据库的用户名
123456 //数据库的密码
jdbc:mysql://localhost:3306/itcast //itcast是数据库名
com.mysql.jdbc.Driver
pom.xml
4.0.0
com.gxj
ts170607
0.0.1-SNAPSHOT
UTF-8
5.0.6.RELEASE
2.5.0
commons-dbcp
commons-dbcp
1.4
junit
junit
4.12
test
org.springframework
spring-core
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-web
5.0.6.RELEASE
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-test
${spring.version}
test
org.mybatis
mybatis
3.2.8
org.mybatis
mybatis-spring
1.2.2
mysql
mysql-connector-java
5.1.6
com.alibaba
druid
1.0.12
org.aspectj
aspectjweaver
1.8.4
log4j
log4j
1.2.17
javax.servlet
servlet-api
3.0-alpha-1
provided
javax.servlet
jstl
1.2
org.codehaus.jackson
jackson-mapper-asl
1.9.13
com.alibaba
fastjson
1.2.3
com.fasterxml.jackson.core
jackson-annotations
2.9.4
com.fasterxml.jackson.core
jackson-core
2.9.4
com.fasterxml.jackson.core
jackson-databind
2.9.4
commons-io
commons-io
2.4
commons-fileupload
commons-fileupload
1.2.2
org.codehaus.jackson
com.springsource.org.codehaus.jackson.mapper
1.4.3
org.codehaus.jackson
com.springsource.org.codehaus.jackson
1.4.3
org.apache.maven.plugins
maven-compiler-plugin
1.8
数据库资源需自己手动创建
到此Spring+jdbc的增删改查已经弄完