发表于: 2018-02-26 21:01:37
2 539
今天完成的事情:(一定要写非常细致的内容,比如说学会了盒子模型,了解了Margin)
今天跑了一下简单的heibernate demo
这是项目结构:
实体类entity,实体类需要打上相应的标签:
package com.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Repository;
import javax.persistence.*;
/**
* @author: Arike
* @program: hibernate1
* @description: 人类
* @create: 2018/2/26 11:22
*/
@Data
@Entity
@Table(name = "man")
@Repository
@AllArgsConstructor
@NoArgsConstructor
public class Man {
@Id
@GeneratedValue
private Long id;
@Column(name = "name")
private String name;
@Column(name = "age")
private Integer age;
}
然后是dao:
package com.dao;
import com.bean.Man;
/**
* @author: Arike
* @program: hibernate1
* @description: dao接口
* @create: 2018/2/26 17:03
*/
public interface ManDao {
Man get(Long id);
Long save(Man man);
}
dao实现类:
package com.dao;
import com.bean.Man;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
/**
* @author: Arike
* @program: hibernate1
* @description:
* @create: 2018/2/26 17:47
*/
@Repository
public class ManDaoImpl implements ManDao {
@Autowired
private SessionFactory sessionFactory;
private Session getCurrentSession() {
return this.sessionFactory.openSession();
}
@Override
public Man get(Long id) {
return (Man)getCurrentSession().get(Man.class,id);
}
@Override
public Long save(Man man) {
return (Long)getCurrentSession().save(man);
}
}
配置文件:
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<context:component-scan base-package="com"/>
<mvc:annotation-driven/>
<context:property-placeholder location="classpath:driud.properties" system-properties-mode="NEVER" ignore-unresolvable="true"/>
<!--配置druid连接池-->
<bean id="driud" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${driverClassName}"/>
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${initialSize}"/>
<!-- 连接池最大使用连接数量 -->
<property name="maxActive" value="${maxActive}"/>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${minIdle}"/>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${maxWait}"/>
<property name="validationQuery" value="${validationQuery}"/>
<property name="testOnBorrow" value="false"/>
<property name="testOnReturn" value="false"/>
<property name="testWhileIdle" value="true"/>
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="252000"/>
<!-- 打开removeAbandoned功能 -->
<property name="removeAbandoned" value="true"/>
<!-- 1800秒,也就是30分钟 -->
<property name="removeAbandonedTimeout" value="1800"/>
<!-- 关闭abanded连接时输出错误日志 -->
<property name="logAbandoned" value="true"/>
<!-- 监控数据库 -->
<!-- <property name="filters" value="stat" /> -->
<property name="filters" value="mergeStat"/>
</bean>
<!--配置session工厂-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="driud" />
<property name="packagesToScan" value="com" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<!--hibernate根据实体自动生成数据库表-->
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<!--指定数据库方言-->
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<!--在控制台显示执行的数据库操作语句-->
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<!--在控制台显示执行的数据哭操作语句(格式)-->
</props>
</property>
</bean>
<!-- 事物管理器配置 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
相应的perporties,数据库配置和基本的hibernate配置.
#key=value
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://rm-wz92ng7ov2t5g84ho3o.mysql.rds.aliyuncs.com:3306/jnshutask?useSSL=false&useUnicode=true&characterEncoding=utf-8
username=root
password=*********
initialSize=5
maxActive=20
minIdle=0
maxWait=60000
validationQuery=SELECT 1
#hibernate config
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.show_sql = true
hibernate.format_sql = true
hibernate.hbm2ddl.auto = update
测试类测试一下:
package com.dao;
import com.bean.Man;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ContextConfiguration("classpath:xml/springmybatis.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class ManDaoImplTest {
@Autowired
ManDaoImpl manDaoImpl;
@Test
public void get() {
System.out.println(manDaoImpl.get(1L));
}
@Test
public void save() {
Man man = new Man();
man.setAge(11);
man.setName("你好");
man.setId(1L);
manDaoImpl.save(man);
}
}
首先执行一下存储,我在之前测试的时候已经帮我默认添加到了第五条数据,表也是自动生成的.
现在再跑一次存储:
控制台运行成功,然后接下来看一下数据库
可以看到已经添加了第六条数据,然后我们测试一下查询语句:
明天计划的事情:(一定要写非常细致的内容)
学习公司框架.
遇到的问题:(遇到什么困难,怎么解决的)
测试类标签打错.
@ContextConfiguration
正确的应该是这个,结果我打成了
@Configuration
就让我想起了做到任务二测试类就废了的事情, 原来那会儿也是这个单词打错了..
收获:(通过今天的学习,学到了什么知识)
学会了hibernate的使用.
评论