1. 用户记录
package auj.helpdesk.model;
Run Code Online (Sandbox Code Playgroud)package auj.helpdesk.model; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class UserRecord { @Id private int id; private String name; private String email; //default conatructor public UserRecord() { } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } } …
我有一个Microsoft和Sybase存储过程,返回结果为"return @value".我需要通过SimpleJdbcCall从Java读取值.
可能吗?
我正在使用SimpleJdbcTemplate从数据库中获取记录列表.每条记录对应一个数据模型对象.我在互联网上搜索了很多,但我仍然无法弄清楚如何使用通用行映射器从数据库中获取多行.
假设我需要获取SampleObj类型的对象列表.
我如何定义参数化行映射器?我如何获得SampleObj类型对象的列表?
每当我尝试创建行映射器时,行映射器中的返回类型始终被检测为单个对象而不是列表.请帮忙.
使用java/Spring/Ibatis sqlserver的数据库,并datasource在org.apache.commons.dbcp.BasicDataSource下面的数据源对象我要揭露实时连接池指望像现在有多少人在使用,有多少闲置的,我想使用监视jmx任何快速的想法如何实现
<bean id="wssModelDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="net.sourceforge.jtds.jdbcx.JtdsDataSource"/>
<property name="url" value="com.wss.jdbc.ConnectionUrl=jdbc:jtds:sqlserver://x-x2/x_control_QA;appName=wss;sendStringParametersAsUnicode=false;loginTimeout=20;socketTimeout=180"/>
<property name="username" value="xxx"/>
<property name="password" value="xxx"/>
<property name="maxActive" value="10"/>
<property name="maxWait" value="10000"/>
</bean>
Run Code Online (Sandbox Code Playgroud) 我正在使用H2数据库来提高性能,因此我决定在运行时将数据从MySQL加载到H2数据库.
场景是我创建了三个表作为book,author,book_author.book_author是一个多对多表.
表格书,作者和book_author分别记录了1000,50000和50000.
选择查询
select book.name
from book, author , book_author
where book.id = book_author.book_id
and book_author.author_id = author.id
and author.name = 'Charles Dickens'
Run Code Online (Sandbox Code Playgroud)
需要7分钟才能执行.
我使用spring-jdbc来创建H2内存数据库.
EmbeddedDatabase database_01 = new EmbeddedDatabaseBuilder().
setType(EmbeddedDatabaseType.H2).
addScript("initial_script.sql").
setName("database_01").build();
JdbcTemplate jdbcTemplate_01 = new JdbcTemplate(database_01);
Run Code Online (Sandbox Code Playgroud)
有人可以建议为什么要花费这么多时间并优化它?
使用带有两个表的postgresql db来验证登录...(用户和权限)
CREATE TABLE users
(
username character(50) NOT NULL,
password character(50) NOT NULL,
enabled boolean NOT NULL,
CONSTRAINT users_pkey PRIMARY KEY (username)
)
CREATE TABLE authorities
(
username character(50) NOT NULL,
authority character(50) NOT NULL,
CONSTRAINT fk_authorities_users FOREIGN KEY (username)
REFERENCES users (username) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
Run Code Online (Sandbox Code Playgroud)
当我尝试执行以下操作时,我做错了什么?
registerAuthentication(AuthenticationManagerBuilder auth){
auth.
jdbcAuthentication()
.dataSource(dataSource)
.usersByUsernameQuery("select username,password,enabled
from users where username = ?")
.authoritiesByUsernameQuery("select username,authority from
authorities where username = ?");
}
Run Code Online (Sandbox Code Playgroud)
谢谢
我试图在Spring中运行DAO类的TestNG测试.但是,尽管有注释,DataSource引用仍未自动装配.以下是测试的一部分:
@ContextConfiguration(locations={"classpath:WEB-INF/servlet-context.xml"})
public class ToDoDaoImplTest extends AbstractTestNGSpringContextTests {
@Autowired
// Construction of this object fails
private ToDoItemDaoImpl toDoDao;
}
Run Code Online (Sandbox Code Playgroud)
这是我的Spring配置:
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- DataSource to be injected -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:mem:test" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
<jdbc:initialize-database data-source="dataSource" ignore-failures="DROPS">
<jdbc:script location="classpath:db.create.sql" />
</jdbc:initialize-database>
<context:component-scan base-package="org.myapp"/>
Run Code Online (Sandbox Code Playgroud)
DAO类:
@Repository
public class ToDoItemDaoImpl implements ToDoItemDao {
private DataSource dataSource;
private SimpleJdbcInsert insert;
public …Run Code Online (Sandbox Code Playgroud) 我的目标是进行集成测试,确保在查找期间不会发生太多数据库查询.(这有助于我们捕获由于错误的JPA配置导致的n + 1个查询)
我知道数据库连接是正确的,因为测试运行期间没有配置问题,只要测试MyDataSourceWrapperConfiguration中没有包含.但是,一旦添加,就会发生循环依赖.(请参阅下面的错误)我认为@Primary有必要让JPA/JDBC代码使用正确的DataSource实例.
MyDataSourceWrapper是一个自定义类,它跟踪给定事务发生的查询数,但它将实际数据库工作委托给DataSource传入的via构造函数.
错误:
The dependencies of some of the beans in the application context form a cycle:
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
???????
| databaseQueryCounterProxyDataSource defined in me.testsupport.database.MyDataSourceWrapperConfiguration
? ?
| dataSource defined in org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Tomcat
? ?
| dataSourceInitializer
???????
Run Code Online (Sandbox Code Playgroud)
我的配置:
@Configuration
public class MyDataSourceWrapperConfiguration {
@Primary
@Bean
DataSource databaseQueryCounterProxyDataSource(final DataSource delegate) {
return MyDataSourceWrapper(delegate);
}
}
Run Code Online (Sandbox Code Playgroud)
我的测试:
@ActiveProfiles({ "it" })
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration({ DatabaseConnectionConfiguration.class, DatabaseQueryCounterConfiguration.class })
@EnableAutoConfiguration
public class EngApplicationRepositoryIT {
@Rule …Run Code Online (Sandbox Code Playgroud) 我想一次删除多个数据库条目。仅当3个字段匹配时(此处:姓名,电子邮件地址,年龄),才应删除每个条目。
如果我只想删除一个属性,则可以:
String sql = "DELETE FROM persons WHERE (email) IN (?)";
JdbcTemplate template;
template.execute(sql, Arrays.asList(emails...));
Run Code Online (Sandbox Code Playgroud)
但是,如果我的病情是由多个领域构成的,那该怎么办?
String sql = "DELETE FROM persons WHERE (name, email, age) IN (?, ?, ?)";
JdbcTemplate template;
template.execute(sql, ...); ???
Run Code Online (Sandbox Code Playgroud)
条件应始终匹配所有3个字段(AND)!
我为我的Spring应用程序使用H2数据库.问题是,一切正常.我可以访问,存储和重新连接到数据库.但是,我收到了IDE的警告
无法解析类或包H2
在这
spring.datasource.driver-class-name=org.h2.Driver
Run Code Online (Sandbox Code Playgroud)
这有点让人困惑,因为我不知道是否以及如何处理这个问题.这曾经是一个类似的问题,但答案没有帮助,我不知道我是否可以再次提出这个问题,或者我是否应该在现有的帖子中发布我的问题?
这是我的application.properties
spring.datasource.url=jdbc:h2:file:~/afile;DB_CLOSE_ON_EXIT=FALSE;AUTO_RECONNECT=TRUE
spring.datasource.username=no:)
spring.datasource.password=no:)
spring.datasource.driver-class-name=org.h2.Driver
#spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
Run Code Online (Sandbox Code Playgroud)
我的POM
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>10</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
<version>2.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.197</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId> …Run Code Online (Sandbox Code Playgroud) spring-jdbc ×10
spring ×5
java ×4
spring-boot ×3
h2 ×2
dao ×1
jdbc ×1
jpa ×1
maven ×1
spring-jmx ×1
sql ×1
sql-server ×1
sybase ×1
testng ×1