[Java] Mirage-Basic usage of SQL

I tried using Mirage-SQL as a framework for data access in a Java application, so I will introduce the basic usage.

Maven settings

pom.xml


  <dependency>
    <groupId>jp.sf.amateras.mirage</groupId>
    <artifactId>mirage</artifactId>
    <version>***</version>
  </dependency>

Also add a repository.

pom.xml


  <repository>
    <id>amateras</id>
    <name>amateras</name>
    <url>http://amateras.sourceforge.jp/mvn</url>
  </repository>

Connection settings to DB

Place the jdbc.properties file at the root of your classpath.

jdbc.properties


jdbc.driver=Driver class
jdbc.url=URL
jdbc.user=User name
jdbc.password=password

SQL file

Place the .sql file at the root of your classpath.

test.sql


SELECT *
FROM staff
 
/*BEGIN*/
WHERE
 
  /*IF username != null */
  staffname = /*staffname*/'testuser'
  /*END*/
 
  /*IF staffstatus != null */
  AND staffstatus = /*status*/1
  /*END*/
 
/*END*/

There are meaningful comments in between. This comment statement and the value after the comment are replaced with placeholders when the prepared statement is generated.

Since the bind variable is written as a comment of SQL and the value for testing is written immediately after that, this SQL can be executed as it is with the DB management tool or the command line. This is "2Way SQL".

Generally, when you operate a database from an application, you think that SQL is stored in a string type variable and executed, but Mirage-SQL can completely separate SQL from Java source code. Therefore, you can avoid the situation that "SQL cannot be confirmed until you try to execute it". Since SQL statements are not assembled dynamically, it is also a countermeasure against SQL injection.

It's a good idea to think of / * BEGIN * / ~ / * END * / as something that adjusts so that no syntax error occurs. (See github wiki for more information)

Entity class

Staff


import jp.sf.amateras.mirage.annotation.Column;
import jp.sf.amateras.mirage.annotation.Table;

@Table(name = "staff")//Can be omitted if the class name and table name are the same
public class Staff implements Serializable {
 
  @Column(name = "staffname")//Can be omitted if the column name and field name are the same
  private String staffname ;
 
  @Column(name = "staffstatus ")
  private int staffstatus ;

  //Below, the accessor method
  public void setStaffname (String staffname) {
    this.staffname= staffname;
  }

  public String getStaffname () {
    return staffname;
  }

  public void setStaffstatus (int staffstatus ) {
    this.staffstatus = staffstatus ;
  }

  public int getStaffstatus () {
    return staffstatus ;
  }

}

Data acquisition

Main



Session session = SessionFactory.getSession();
SqlManager sqlManager = session.getSqlManager();

session.begin();
try {
	Map<String, Object> params = new HashMap<>();
	params.put("status", 0);
	SqlResource selectUserSql = new ClasspathSqlResource("test.sql");
	List<Staff> results = sqlManager.getResultList(Staff.class, selectUserSql, params);

	for (Staff result : results ) {
		System.out.println(result.getStaffname());
	}
	session.commit();
} catch (Exception ex) {
	session.rollback();
} finally {
	session.release();
}

I used Map as the value to pass to the bind variable, but a class with the same name as the bind variable is fine.

Recommended Posts

[Java] Mirage-Basic usage of SQL
Basic usage of java Optional Part 1
Examine the memory usage of Java elements
[Java] Overview of Java
Java review ③ (Basic usage of arrays / reference type)
Expired collection of java
Predicted Features of Java
NIO.2 review of java
Review of java Shilber
java --Unification of comments
Minimal usage of Mockito
Regarding Java variable usage
History of Java annotation
java (merits of polymorphism)
NIO review of java
[Java] Three features of Java
Summary of Java support 2018
About an instance of java
[Java] Beginner's understanding of Servlet-②
Basic knowledge of SQL statements
[Java] Practice of exception handling [Exception]
[Java11] Stream Summary -Advantages of Stream-
Basics of character operation (java)
[Practice! 】 Execution of SQL statement
[Java] Creation of original annotation
4th day of java learning
[Java] Beginner's understanding of Servlet-①
Java end of month plusMonths
[Java] Summary of regular expressions
[Java] Summary of operators (operator)
[Java] Implementation of Faistel Network
[Java] Comparator of Collection class
Java exception handling usage rules
Summary of Java language basics
Super basic usage of Eclipse
[Java] Summary of for statements
Summary of Java Math class
Enumeration of all combinations Java
java (inheritance of is-a principle)
Implementation of gzip in java
Advantages and disadvantages of Java
Benefits of Java static method
[Java11] Stream Usage Summary -Basics-
[Java] Summary of control syntax
Implementation of tri-tree in Java
Summary of java error processing
[Java] Summary of design patterns
[Java] Summary of mathematical operations
[Java] Speed comparison of string concatenation
Think of a Java update strategy
[Java] Delete the elements of List
[For beginners] Summary of java constructor
Various methods of Java String class
JCA (Java Cryptography Architecture) Usage Memo
Root cause of java framework bugs
About fastqc of Biocontainers and Java
[Java version] The story of serialization
Summary of [Java silver study] package
About Lambda, Stream, LocalDate of Java8
Story of passing Java Gold SE8
Sort a List of Java objects