这是开发六步第二部,写dao层。
首先dao层实在model层之上建立的,我重复一下之前创建的表代码,在id上和hobby字段上改了之前的long 为String ,String[]数组为 String。
1.Model 层的Studentsinf代码为:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import org.hibernate.annotations.OptimisticLockType;
/**
* Java Persistence API定义了一种定义,可以将常规的普通Java对象(有时被称作POJO)映射到数据库。
* 这些普通Java对象被称作Entity Bean
*
* @Entity说明这个class是实体类,并且使用默认的orm规则,即
* class名 ---> 数据库表中表名,
* class字段名 ---> 表中的字段名
*
* 如果想改变这种默认的orm规则,就要使用@Table来改变class名与数据库中表名的映射规则,
* @Table(name = "T_TEXT_TRANING_COMPANY") ,name 后面的是表名
* @id表示主键
* @Column来改变 class中字段名 ---->db中表的字段名 的映射规则
*/
@Entity
@Table(name="T_TRAINING_CHEN_STUDENTS")
@org.hibernate.annotations.Entity(optimisticLock = OptimisticLockType.VERSION)
public class Studentsinf {
private static final long serialVersionUID = -2861741637268711367L;
/**
* ID
*/
private String id;
/**
* 名字
*/
private String name;
/**
* 年龄
*/
private int age;
/**
* 性别
*/
private String legend;
/**
* 省份
*/
private String province;
/**
* 组别
*/
private String team;
/**
* 爱好
*/
private String hobby;
@Id
@Column(name = "ID")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Column(name = "name")
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
@Column(name = "age")
public int getAge(){
return age;
}
public void setAge(int age){
this.age = age;
}
@Column(name = "legend")
public String getLegend(){
return legend;
}
public void setLegend(String legend){
this.legend = legend;
}
@Column(name = "province")
public String getProvince(){
return province;
}
public void setProvince(String province){
this.province = province;
}
@Column(name = "team")
public String getTeam(){
return team;
}
public void setTeam(String team){
this.team = team;
}
@Column(name = "hobby")
public String getHobby(){
return hobby;
}
public void setHobby(String hobby){
this.hobby = hobby;
}
@Override
public String toString(){
return "Studentsinf [id=" + id + ", name=" + name + ", age=" + age + ", legend=" + legend + ", province=" + province + ", team=" + team + ", hobby=" + hobby + "]";
}
}
2.Dao层创建查询的方法,代码为:
public interface StudentsinfDao extends GenericEntityDao<Studentsinf, Long>{
/** 查询所有的学生信息 */
@NativeQuery(model = Studentsinf.class)
List<Studentsinf> findAllStudentsinf();
/** 根据id查询学生信息 */
@NativeQuery(model = Studentsinf.class)
Studentsinf findStudentsinfById(@QueryParam("id") String id);
}
3.写数据库语句
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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">
<bean id="Studentsinf.findAllStudentsinf" class="根据自己路径设置">
<description> 找到所有的Students的信息</description>
<constructor-arg>
<value>
<![CDATA[
select *
from t_training_chen_students
]]>
</value>
</constructor-arg>
</bean>
<bean id="Studentsinf.findStudentsinfById" class="loxia.dao.support.DynamicQueryHolder">
<description>通过ID查询Students的信息</description>
<constructor-arg>
<value>
<![CDATA[
select *
from t_training_chen_students
where id = :id
]]>
</value>
</constructor-arg>
</bean>
</beans>
4.对dao层进行测试
public class StudentsinfDaoTest extends BaseTransactionalJUnit4SpringContextTests{
private static final Logger LOGGER = LoggerFactory.getLogger(StudentsinfDaoTest.class);
@Autowired
private StudentsinfDao studentsinf;
@Test
public void testFindAllStudentsinf() {
List<Studentsinf> list = studentsinf.findAllStudentsinf();
Studentsinf si = null;
LOGGER.info("----------------------- 开始测试 -----------------------");
for(int i = 0; i < list.size(); i++) {
si = list.get(i);
LOGGER.debug("" + JSON.toJSONString(si));
LOGGER.debug("--" + si.toString());
}
LOGGER.info("----------------------- 结束测试 -----------------------");
}
@Test
public void testFindStudentsinfById() {
Studentsinf sinf = studentsinf.findStudentsinfById("JMSH10393");
LOGGER.info("----------------------- 开始测试 -----------------------");
LOGGER.debug("" + JSON.toJSONString(sinf));
LOGGER.info("----------------------- 结束测试 -----------------------");
}
}