T queryForObject(String sql,RowMapper rowMapper,Object ... args);
(4)查询:返回集合
List query(String sql,RowMapper rowMapper,Object... args);
(5)批量增删改:
int[] batchUpdate(String sql,List
1.2、举例:
引入相关jar包
org.springframeworkspring-webmvc5.1.8.RELEASE
配置数据库连接池;配置JdbcTemplate对象
创建Service类和Dao类,在Dao类中注入JdbcTemplate对象
public interface BookDao {
public void add(Book book); //添加图书
public void update(Book book); //修改图书
public void delete(int id); //删除图书
public int queryCount(); //查询数量
public Book queryBookById(int id); //查询某本书
public List queryBooks(); //查询所有书
public void batchAddBook(List
@Repository
public class BookDaoImpl implements BookDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void add(Book book) {
String sql = "insert into t_book set name=?,price=?";
Object[] args = {book.getBookName(),book.getBookPrice()};
int update = jdbcTemplate.update(sql, args);
System.out.println(update);
}
@Override
public void update(Book book) {
String sql = "update t_book set name=?,price=? where id=?";
Object[] args = {book.getBookName(),book.getBookPrice(),book.getBookId()};
int update = jdbcTemplate.update(sql, args);
System.out.println(update);
}
@Override
public void delete(int id) {
String sql = "delete from t_book where id=?";
int update = jdbcTemplate.update(sql, id);
System.out.println(update);
}
@Override
public int queryCount() {
String sql = "select count(*) from t_book";
Integer count = jdbcTemplate.queryForObject(sql, Integer.class);
return count;
}
@Override
public Book queryBookById(int id) {
String sql = "select id bookId,name bookName,price bookPrice from t_book where id=?";
Book book = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper(Book.class), id);
return book;
}
@Override
public List queryBooks() {
String sql = "select id bookId,name bookName,price bookPrice from t_book";
List bookList = jdbcTemplate.query(sql, new BeanPropertyRowMapper(Book.class));
return bookList;
}
@Override
public void batchAddBook(List books) {
String sql = "insert into t_book set id=?,name=?,price=?";
int[] ints = jdbcTemplate.batchUpdate(sql, books);
System.out.println(ints);
}
@Override
public void batchUpdateBook(List books) {
String sql = "update t_book set name=?,price=? where id=?";
int[] ints = jdbcTemplate.batchUpdate(sql, books);
System.out.println(ints);
}
@Override
public void batchDeleteBook(List args) {
String sql = "delete from t_book where id=?";
int[] ints = jdbcTemplate.batchUpdate(sql, args);
System.out.println(ints);
}
}
@Service
public class BookService {
@Autowired
private BookDao bookDao = new BookDaoImpl();
//添加图书
public void add(Book book){
bookDao.add(book);
}
//修改图书
public void update(Book book){
bookDao.update(book);
}
//删除图书
public void delete(Integer id){
bookDao.delete(id);
}
//查询数量
public int queryCount(){
return bookDao.queryCount();
}
//查询图书
public Book queryBookById(Integer id){
return bookDao.queryBookById(id);
}
//查询所有图书
public List queryBooks(){
return bookDao.queryBooks();
}
//批量添加图书
public void batchAddBook(List books){
bookDao.batchAddBook(books);
}
//批量修改图书
public void batchUpdateBook(List books){
bookDao.batchUpdateBook(books);
}
//批量删除图书
public void batchDeleteBook(List args){
bookDao.batchDeleteBook(args);
}
}
public interface UserDao {
//多钱
public void addMoney();
//少钱
public void reduceMoney();
}
@Repository
public class UserDaoImpl implements UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void addMoney() {
String sql = "update user_db set money = money + ? where username = ?";
jdbcTemplate.update(sql,100,"mary");
}
@Override
public void reduceMoney() {
String sql = "update user_db set money = money - ? where username = ?";
jdbcTemplate.update(sql,100,"lucy");
}
}
@Service
public class UserService {
//注入dao
@Autowired
private UserDao userDao;
//转账
public void accountMoney(){
//lucy少100
userDao.reduceMoney();
//mary多100
userDao.addMoney();
}
}
public class TestBook {
@Test
public void testAccount(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.accountMoney();
}
}
上述代码,如果正常执行没有问题,但是如果代码执行过程中,出现异常会有问题。
问题例如:
@Service
public class UserService {
//注入dao
@Autowired
private UserDao userDao;
//转账
public void accountMoney(){
//lucy少100
userDao.reduceMoney();
//模拟异常
int i = 100/0;
//mary多100
userDao.addMoney();
}
}
如何解决:
使用事务,事务过程:
@Service
public class UserService {
//注入dao
@Autowired
private UserDao userDao;
//转账
public void accountMoney(){
try {
//1、开启事务
//2、处理逻辑
//lucy少100
userDao.reduceMoney();
//模拟异常
int i = 100/0;
//mary多100
userDao.addMoney();
//3、没有异常,提交事务
} catch (Exception e) {
//4、出现异常,事务回滚
}
}
}
@RunWith(SpringJUnit4ClassRunner.class) //单元测试框架版本
@ContextConfiguration("classpath:bean4.xml") //加载配置文件
public class JUnitTest {
@Autowired
public User user;
@Test
public void test(){
System.out.println(user);
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml&q
1. 安装memcached server
a. 下载memcached-1.2.6-win32-bin.zip
b. 解压缩,dos 窗口切换到 memcached.exe所在目录,运行memcached.exe -d install
c.启动memcached Server,直接在dos窗口键入 net start "memcached Server&quo
Log4j组件:Logger、Appender、Layout
Log4j核心包含三个组件:logger、appender和layout。这三个组件协作提供日志功能:
日志的输出目标
日志的输出格式
日志的输出级别(是否抑制日志的输出)
logger继承特性
A logger is said to be an ancestor of anothe
public static void main(String[] args) throws IOException {
//输入流
InputStream in = Test.class.getResourceAsStream("/test");
InputStreamReader isr = new InputStreamReader(in);
Bu
对于那些具有强迫症的工程师来说,软件汉化固然好用,但是汉化不完整却极为头疼,本方法针对iReport汉化不完整的情况,强制使用英文版,方法如下:
在 iReport 安装路径下的 etc/ireport.conf 里增加红色部分启动参数,即可变为英文版。
# ${HOME} will be replaced by user home directory accordin
网上找了很久,都是用Gallery实现的,效果不是很满意,结果发现这个用OpenGL实现的,稍微修改了一下源码,实现了无限循环功能
源码地址:
https://github.com/jackfengji/glcoverflow
public class CoverFlowOpenGL extends GLSurfaceView implements
GLSurfaceV