我愿意以后学习ORM。但现在我想使用纯SQL代码来管理我的Java Web应用程序的数据层,并且不希望任何类似ORM的框架。它的最佳方法或实践是什么?
答案 0 :(得分:0)
建议尝试Spring-JDBC
。它很容易启动和用户。见下面的例子。
以下是获取关系中行数的简单查询:
不要忘记应该注射jdbcTemplate
。
int rowCount = jdbcTemplate.queryForObject("select count(*) from t_actor", Integer.class);
使用下面的SQL:
SELECT * FROM BOOK
WHERE BOOK.PUBLISHED_IN = 2011
ORDER BY BOOK.TITLE
java
jOOQ
使用create.selectFrom(BOOK)
.where(BOOK.PUBLISHED_IN.eq(2011))
.orderBy(BOOK.TITLE)
:
SQL
两者都很方便。但是关于 pure Spring JDBC
的使用 - 我建议开始使用JDBC
。
如果你正在开始你的道路 - 强烈建议开始使用普通的public class Student {
private String name;
private int rollNo;
Student(String name, int rollNo){
this.name = name;
this.rollNo = rollNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRollNo() {
return rollNo;
}
public void setRollNo(int rollNo) {
this.rollNo = rollNo;
}
}
,以便熟悉框架内部运行的样板代码。
DAO (数据访问对象)是一个通常具有 CRUD (创建,读取,更新,删除)操作的类 - 保存,阅读,更新,删除。
DTO (数据传输对象)只是一个包含数据的对象。它是一个带有实例变量和setter和getter的JavaBean。通常是传递给DAO的save方法的DTO。
<强> Student.java 强>
public interface StudentDao {
List<Student> getAllStudents();
Student getStudent(int rollNo);
void updateStudent(Student student);
void deleteStudent(Student student);
}
<强> StudentDao.java 强>
public class StudentDaoImpl implements StudentDao {
//list is working as a database
private static final List<Student> students;
public StudentDaoImpl() {
students = new ArrayList<Student>();
students.add(new Student("Student 0", 0));
students.add(new Student("Student 1", 1));
}
@Override
public void deleteStudent(Student student) {
students.remove(student.getRollNo());
System.out.println("Student: Roll No " + student.getRollNo() + ", deleted from database");
}
@Override
public List<Student> getAllStudents() {
return students;
}
@Override
public Student getStudent(int rollNo) {
return students.get(rollNo);
}
@Override
public void updateStudent(Student student) {
Student foundStudent = students.get(student.getRollNo())
foundStudent.setName(student.getName());
System.out.println("Student: Roll No " + student.getRollNo() + ", updated in the database");
}
}
<强> StudentDaoImpl.java 强>
public class DaoPatternDemo {
public static void main(String[] args) {
StudentDao studentDao = new StudentDaoImpl();
for (Student student : studentDao.getAllStudents()) {
System.out.println("Student: [RollNo : " + student.getRollNo() + ", Name : " + student.getName() + " ]");
}
Student student = studentDao.getAllStudents().get(0);
student.setName("Michael"); // here student is our DTO because we are
// passing it into our DAO
studentDao.updateStudent(student);
studentDao.getStudent(0);
System.out.println("Student: [RollNo : " + student.getRollNo() + ", Name : " + student.getName() + " ]");
}
}
<强> DaoPatternDemo.java 强>
<?php
require_once '../PHPWord.php';
// New Word Document
$PHPWord = new PHPWord();
// New portrait section
$section = $PHPWord->createSection();
// Add hyperlink elements
$section->addLink('http://www.google.com', 'Best search engine', array('color'=>'0000FF', 'underline'=>PHPWord_Style_Font::UNDERLINE_SINGLE));
$section->addTextBreak(2);
$PHPWord->addLinkStyle('myOwnLinkStyle', array('bold'=>true, 'color'=>'808000'));
$section->addLink('http://www.bing.com', null, 'myOwnLinkStyle');
$section->addLink('http://www.yahoo.com', null, 'myOwnLinkStyle');
// Save File
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('Link.docx');
?>
要了解详情,请访问此resource。