【Java Web开发学习】Spring JPA
【Java Web开发学习】Spring JPA
转载:https://www.cnblogs.com/yangchongxing/p/10082864.html
1、使用容器管理类型的JPA
JNDI数据源
package cn.ycx.config; import javax.naming.NamingException; import javax.sql.DataSource; import org.springframework.context.annotation.Bean; import org.springframework.jndi.JndiObjectFactoryBean; public class DataSourceConfig { @Bean public DataSource dataSource() throws IllegalArgumentException, NamingException { System.out.println("dataSource..."); JndiObjectFactoryBean jndi = new JndiObjectFactoryBean(); jndi.setJndiName("jdbc/mysql"); jndi.setResourceRef(true);//自动添加 java:comp/env/ 前缀 jndi.setProxyInterface(javax.sql.DataSource.class); jndi.afterPropertiesSet(); return (DataSource) jndi.getObject(); } }
声明LocalContainerEntityManagerFactoryBean
package cn.ycx.config; import javax.sql.DataSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; import org.springframework.context.annotation.Import; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.orm.jpa.JpaVendorAdapter; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.vendor.Database; import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.web.multipart.MultipartResolver; import org.springframework.web.multipart.support.StandardServletMultipartResolver; @Configuration @ComponentScan(basePackages = {"cn.ycx"}, excludeFilters = { @Filter( type=FilterType.ANNOTATION, value=org.springframework.stereotype.Controller.class) }) @Import(DataSourceConfig.class) public class RootConfig { @Bean public MultipartResolver multipartResolver() { System.out.println("multipartResolver..."); return new StandardServletMultipartResolver(); } @Bean public JdbcTemplate jdbcTemplate(DataSource dataSource) { System.out.println("jdbcTemplate..."); return new JdbcTemplate(dataSource); } @Bean public JpaVendorAdapter jpaVendorAdapter() { System.out.println("jpaVendorAdapter..."); HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter(); adapter.setDatabase(Database.MYSQL); adapter.setShowSql(true); adapter.setGenerateDdl(false); adapter.setDatabasePlatform("org.hibernate.dialect.MySQL55Dialect"); return adapter; } @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter ) { System.out.println("localContainerEntityManagerFactoryBean..."); LocalContainerEntityManagerFactoryBean emfb = new LocalContainerEntityManagerFactoryBean(); emfb.setDataSource(dataSource); emfb.setJpaVendorAdapter(jpaVendorAdapter); emfb.setPackagesToScan("cn.ycx.entity"); return emfb; } }
编写Repository
package cn.ycx.dao; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.PersistenceUnit; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcOperations; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; import cn.ycx.entity.User; @Repository public class IndexDao { @Autowired private JdbcOperations jdbcOperations; @PersistenceUnit private EntityManagerFactory emf; public void addUser() { SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS"); User user = new User(); user.setId(format.format(new Date())); user.setUsername("name"); EntityManager em = emf.createEntityManager(); EntityTransaction et = em.getTransaction(); et.begin();//开始事务 em.persist(user); et.commit();//提交事务 } public Object insert() { int count = jdbcOperations.update("insert into user(id,username,password)values(?,?,?)","2","user","user"); Map<String, String> data = new HashMap<String, String>(); data.put("count", String.valueOf(count)); return data; } public Object update() { int count = jdbcOperations.update("update user set username=?, password=? where id=?","admin","admin","1"); Map<String, String> data = new HashMap<String, String>(); data.put("count", String.valueOf(count)); return data; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端