Home | 简体中文 | 繁体中文 | 杂文 | Search | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 作品与服务 | Email

第 7 章 Spring Data

目录

7.1. JdbcTemplate
7.1.1. execute
7.1.2. queryForInt
7.1.3. queryForObject
7.1.4. queryForList
7.2. Example
7.2.1. Redis
7.2.1.1. pom.xml
7.2.1.2. springframework-servlet.xml
7.2.1.3. Controller
7.2.1.4. index.jsp
7.2.1.5. 测试
7.2.2. Spring Data MongoDB
7.2.2.1. pom.xml
7.2.2.2. springframework-servlet.xml
7.2.2.3. POJO
7.2.2.4. Controller
7.2.2.5. 查看测试结果
7.2.2.6. 条件查询
7.3. MongoRepository
7.3.1. findAll()
7.3.2. deleteAll()
7.3.3. save()
7.3.4. count()
7.3.5. PageRequest
7.4. @Entity
7.4.1. @NotNull 不能为空声明
7.4.2. 处理日期时间
7.5. Query
7.6. FAQ
7.6.1. No identifier specified for entity
7.6.2. Oracle Date 类型显示日期和时间

7.1. JdbcTemplate

7.1.1. execute

jdbcTemplate.execute("CREATE TABLE USER (id integer, name varchar(100))");  			
			

7.1.2. queryForInt

int count = jdbcTemplate.queryForInt("SELECT COUNT(*) FROM USER");			
			

7.1.3. queryForObject

	private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);
	private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
	
	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Scheduled(initialDelay = 1000, fixedRate = 60000)
	public void currentDate() {
		Date date = jdbcTemplate.queryForObject("select sysdate from dual", Date.class);
		log.info("The oracle sysdate is {}", dateFormat.format(date));
	}			
			
			
	@Autowired
	private JdbcTemplate jdbcTemplate;

	@RequestMapping(value = "/article")
	public @ResponseBody String dailyStats(@RequestParam Integer id) {
		String query = "SELECT id, title, content from article where id = " + id;

		return jdbcTemplate.queryForObject(query, (resultSet, i) -> {
			System.out.println(resultSet.getLong(1) + "," + resultSet.getString(2) + "," + resultSet.getString(3));
			return (resultSet.getLong(1) + "," + resultSet.getString(2) + "," + resultSet.getString(3));
		});

	}			
			
			
Integer age = queryForObject("select age from emp", Integer.class);
String name = queryForObject("select name from emp",String.class);
			

7.1.4. queryForList

			
List rows = jdbcTemplate.queryForList("SELECT * FROM USER");  
Iterator it = rows.iterator();  
while(it.hasNext()) {  
    Map userMap = (Map) it.next();  
    System.out.print(userMap.get("id") + "\t");  
    System.out.print(userMap.get("name") + "\t");  
    System.out.print(userMap.get("sex") + "\t");  
    System.out.println(userMap.get("age") + "\t");  
}  			
			
			
comments powered by Disqus