SpringBoot+hibernate例子

网上看了好多,留个例子给自己,都是最简单的使用,没有触及到自己写SQL,用的是Hibernate封装好的方法,但是现实中肯定是基础方法不能满足查询语句的。

一、SpringBoot整合Hibernate,看文章:https://blog.csdn.net/nzzl54/article/details/81067807

二、配置完后上例子:

1、html中:


请输入姓名:



请输入id:

2.Controller中:

@Controller
@RequestMapping("/test")
public class testController {
     @Resource
    private TestService testService;
	@Autowired
	private VoDao voDao;
	
	@RequestMapping("/testQuery")
	@ResponseBody
    public Map testQuery(@RequestBody String name) {
		Map m = new HashMap<>();
		try {
			int id = Integer.valueOf(name);
			List list = (List) voDao.findAll();
			System.err.println("sample started. data ="+list);
			m.put("ok", "查询成功");
			m.put("data",voDao.findOne(id));  //这是不太规范的写法,应该要和下面方法一样,进行封装到TestService中
		}catch(Exception e) {
			e.printStackTrace();
			logger.info(e.getStackTrace().toString());
			m.put("error",e.getStackTrace().toString());
		}
		return m;
    }
	
	@RequestMapping("/testHibernate")
	@ResponseBody
    public Map testHibernate(@RequestBody Map m) {
		try {
			int male = Integer.valueOf(m.get("male").toString());
			int age = Integer.valueOf(m.get("age").toString());
			TestVo d =new TestVo(m.get("name").toString(),age,male,"听音乐");
			d.setRemark("这是备注haha");
			testService.save(d);//保存数据
			m.put("ok", "hibernate测试保存成功 !" + m.get("name").toString());
		}catch(Exception e) {
			e.printStackTrace();
			logger.info(e.getStackTrace().toString());
			m.put("error",e.getStackTrace().toString());
		}
		return m;
    }
}

3、写DAO和DTO:

@Entity //加入这个注解,就会进行持久化了
@Table(name="userinfo") //对应数据库的表名字
public class TestVo {
	@Id
	@GeneratedValue
	private Integer id;
	@Column(name="name")
    private String name;
	@Column(name="male")
    private  Integer male;   //性别
	@Column(name="age")
    private  Integer age;   //年龄
	@Column(name="hobit")
    private  String hobit;  //爱好
    @Column(name = "remark")
    private String remark;  //备注
    
    public TestVo() {
    }

    public TestVo(String name, Integer age, Integer male,String hobit) {
        this.name = name;
        this.male = male;
        this.hobit = hobit;
        this.age = age;
    }
    
    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 getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
    
    public Integer getMale() {
        return male;
    }

    public void setMale(Integer male) {
        this.male = male;
    }
    
    public String getHobit() {
        return hobit;
    }

    public void setHobit(String hobit) {
        this.hobit = hobit;
    }
    
    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

}
@Repository  
@Table(name="userinfo")
@Qualifier("voDao")
public interface VoDao extends CrudRepository {
   //别的方法可以自己添加
}

4、写Service和其实现:

public interface TestService {

	 public void save(TestVo t);
	 
}
@Service
public class TestServiceImpl implements TestService{
    
    @Autowired
    private VoDao voDao;
     
    @Override
    public void save(TestVo t) {
        // TODO Auto-generated method stub
        voDao.save(t);
    }
}

5、数据库截图和保存截图

SpringBoot+hibernate例子_第1张图片

SpringBoot+hibernate例子_第2张图片

SpringBoot+hibernate例子_第3张图片

你可能感兴趣的:(web开发)