1.DAO层

package com.leo.dao;

import com.leo.entity.Book;

public interface BookDao {

void add(Book book);
}

package com.leo.dao;

import com.leo.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class BookDaoImpl implements BookDao{

@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void add(Book book) {
String sql = "insert into t_book values(?,?,?)";
Object[] args = {book.getUserId(),book.getUsername(),book.getUstatus()};
int update = jdbcTemplate.update(sql,args);
System.out.println(update);
}

}

2.Entity层

package com.leo.entity;

public class Book {

private String userId;
private String username;
private String ustatus;

public void setUserId(String userId) {
this.userId = userId;
}

public void setUsername(String username) {
this.username = username;
}

public void setUstatus(String ustatus) {
this.ustatus = ustatus;
}

public String getUserId() {
return userId;
}

public String getUsername() {
return username;
}

public String getUstatus() {
return ustatus;
}
}

3.Service层

package com.leo.service;

import com.leo.dao.BookDao;
import com.leo.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BookService {
@Autowired
private BookDao bookDao;

public void addBook(Book book) {
bookDao.add(book);
}
}

4.测试

package com.leo.test;

import com.leo.entity.Book;
import com.leo.service.BookService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestBook {
@Test
public void testJdbcTemplate() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
BookService bookService = context.getBean("bookService", BookService.class);
Book book = new Book();
book.setUserId("100");
book.setUsername("test");
book.setUstatus("ok");

bookService.addBook(book);
}
}

5. 数据库SQL

CREATE TABLE `t_book` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`ustatus` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
PRIMARY KEY (`user_id`)
) ENGINE=MyISAM AUTO_INCREMENT=101 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;