本文中省去了创建User对象并为其做映射的部分
第一步:添加spring和hibernate的jar包;
第二步:创建接口文件
package cleversoft.spring3.h3;
import cleversoft.spring3.h3.user.User;
public interface H3Api {
public boolean create(User u);
}
第三步:创建接口的实现类,并实现getter和setter方法
package cleversoft.spring3.h3;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
import cleversoft.spring3.h3.user.User;
public class Impl implements H3Api{
private SessionFactory sf = null;
public void setSf(SessionFactory sf) {
this.sf = sf;
}
public SessionFactory getSf() {
return sf;
}
public boolean create(User u){
HibernateTemplate ht = new HibernateTemplate(sf);
ht.save(u);
return false;
}
}
第四步:创建客户端验证类
package cleversoft.spring3.h3;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cleversoft.spring3.h3.user.User;
public class Client {
public static void main(String args[]){
ApplicationContext ctx = new ClassPathXmlApplicationContext(
new String[]{"applicationContext.xml"}
);
H3Api api = (H3Api)ctx.getBean("api");
User u = new User();
u.setUuid("1");
u.setName("小强");
u.setAge("20");
api.create(u);
}
}
第五步:配置applicationContext.xml文件,注意数据源和sessionFactory的配置以及它们之间的关系
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean name="api" class="cleversoft.spring3.h3.Impl">
<property name="sf" ref="sessionFactory"></property>
</bean>
<bean id="DataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/user"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="DataSource"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath*:cleversoft/spring3/h3/user</value>
</list>
</property>
</bean>
</beans>
第六步:验证,出现下边的sql语句表示成功
Hibernate: insert into tbl_user (uuid, name, age) values (?, ?, ?)
分享到:
相关推荐
车牌识别项目
python、yolo
Ollama本地模型对话、选择本地文件、本地图像对话 1、新增根据聊天记录回复的功能。 2、优化了部分ViewModel,将对应Model字段、属性移到Model中,方便后续扩展。 3、新增读取外部数据回复问题功能,目前支持txt文件。 4、新增添加图片提问题功能,模型需要支持视觉(如:minicpm-v:latest)。 5、优化了类结构,创建对应的Model(MainWindowModel),将所有字段、属性移到Model。 6、新增聊天记录窗体,修改了窗体加载时,加载聊天记录的功能。将其拆分成一个视图。 7、移除了折叠栏功能,更新为Grid区域的显示与隐藏。 将聊天记录列表从主窗体中分离)。 8、更新记录文件加载功能,显示提问日期。 新增选择文件类型设置预览图标。 9、新增功能,新聊天后第一次提问完成后,保存的记录刷新到记录列表、记录删除功能。 10、新增功能,创建新窗体判断显示Ollama服务运行状态。
车牌识别项目
人工智能、大语言模型相关学习资料
车牌识别项目
图像处理项目实战
P+F安全栅组态软件
图像处理项目实战
图像处理项目实战
车牌识别项目
COMBAT FURY.7z
车牌识别项目
系统选用B/S模式,后端应用springboot框架,前端应用vue框架, MySQL为后台数据库。 本系统基于java设计的各项功能,数据库服务器端采用了Mysql作为后台数据库,使Web与数据库紧密联系起来。 在设计过程中,充分保证了系统代码的良好可读性、实用性、易扩展性、通用性、便于后期维护、操作方便以及页面简洁等特点。
车牌识别项目
这是第2402节课的内容,作为复习资料
采用最新OCR引擎结合优化算法,使得识别性能极大提升!给广大文字工作者带来了福音......
车牌识别项目
系统选用B/S模式,后端应用springboot框架,前端应用vue框架, MySQL为后台数据库。 本系统基于java设计的各项功能,数据库服务器端采用了Mysql作为后台数据库,使Web与数据库紧密联系起来。 在设计过程中,充分保证了系统代码的良好可读性、实用性、易扩展性、通用性、便于后期维护、操作方便以及页面简洁等特点。
车牌识别项目