Spring integrates Mybatis and springmybatis
Jar file: http://download.csdn.net/detail/huhui_bj/8575533
1. Prepare the jar package
[Mybatis]
Mybatis-3.2.0.jar
Mybatis-spring-1.1.1.jar
Log4j-1.2.17.jar
[Spring]
Spring-aop-3.2.0.RELEASE.jar
Spring-beans-3.2.0.RELEASE.jar
Spring-context-3.2.0.RELEASE.jar
Spring-core-3.2.0.RELEASE.jar
Spring-expression-3.2.0.RELEASE.jar
Spring-jdbc-3.2.0.RELEASE.jar
Spring-test-3.2.4.RELEASE.jar
Spring-tx-3.2.0.RELEASE.jar
Aopalliance-1.0.jar
Cglib-nodep-2.2.3.jar
Commons-logging-1.1.1.jar
MYSQL driver package]
Mysql-connector-java-5.0.4-bin.jar
2. Create data tables and entity classes
CREATE TABLE s_user(user_id INT AUTO_INCREMENT PRIMARY KEY,user_name VARCHAR(30),user_birthday DATE,user_salary DOUBLE)
Public class User {private int id; private String name; private Date birthday; private double salary; // set, get method}
Iii. DAO Interface
public interface UserMapper {void save(User user);void update(User user);void delete(int id);User findById(int id);List<User> findAll();}
Iv. SQL ing file: userMapper. xml (the same name as the interface case-insensitive)
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE mapper PUBLIC "-// mybatis.org//DTD Mapper 3.0 //" http://mybatis.org/dtd/mybatis-3-mapper.dtd "> <! -- UserMapper. the java file name must be the same as userMapper. same xml namespace: must be consistent with the full Class Name of the corresponding interface id: must be consistent with a method name of the corresponding interface --> <mapper namespace = "com. atguigu. mybatis. test9.UserMapper "> <resultMap type =" User "id =" userResult "> <result column =" user_id "property =" id "/> <result column =" user_name "property =" name "/> <result column =" user_birthday "property =" birthday "/> <result column =" user_salary "property =" salary "/> </resultMap> <! -- Get the id of the inserted data --> <insert id = "save" keyColumn = "user_id" keyProperty = "id" useGeneratedKeys = "true"> insert into s_user (user_name, user_birthday, user_salary) values (# {name },# {birthday },#{ salary}) </insert> <update id = "update"> update s_userset user_name =#{ name }, user_birthday =#{ birthday }, user_salary =#{ salary} where user_id =#{ id} </update> <delete id = "delete"> delete from s_userwhere user_id =#{ id} </delete> <select id = "findById" resultMap = "userResult"> select * from s_userwhere user_id =#{ id} </select> <select id = "findAll" resultMap = "userResult"> select * from s_user </select> </mapper>
5. Spring configuration file beans. xml
<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: p = "http://www.springframework.org/schema/p" xmlns: context = "http://www.springframework.org/schema/context" xmlns: tx = "http://www.springframework.org/schema/tx" xsi: schemaLocation = "http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.2.xsd Http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <! -- 1. data source: DriverManagerDataSource --> <bean id = "dataSource" class = "org. springframework. jdbc. datasource. driverManagerDataSource "> <property name =" driverClassName "value =" com. mysql. jdbc. driver "/> <property name =" url "value =" jdbc: mysql: // localhost: 3306/mybatis "/> <property name =" username "value =" root "/> <property name =" password "value =" root "/> </bean> <! -- 2. sqlSession factory of mybatis: SqlSessionFactoryBean dataSource/typeAliasesPackage --> <bean id = "sqlSessionFactory" class = "org. mybatis. spring. sqlSessionFactoryBean "> <property name =" dataSource "ref =" dataSource "/> <property name =" typeAliasesPackage "value =" com. atuigu. spring_mybatis2.domain "/> </bean> <! -- 3. mybatis automatically scans and loads the SQL ing file: MapperScannerConfigurer sqlSessionFactory/basePackage --> <bean class = "org. mybatis. spring. mapper. mapperScannerConfigurer "> <property name =" basePackage "value =" com. atuigu. spring_mybatis2.mapper "/> <property name =" sqlSessionFactory "ref =" sqlSessionFactory "/> </bean> <! -- 4. transaction Management: DataSourceTransactionManager --> <bean id = "txManager" class = "org. springframework. jdbc. datasource. dataSourceTransactionManager "> <property name =" dataSource "ref =" dataSource "/> </bean> <! -- 5. Use declarative transactions --> <tx: annotation-driven transaction-manager = "txManager"/> </beans>
6. mybatis configuration file: mybatis-config.xml
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE configuration PUBLIC "-// mybatis.org//DTD Config 3.0 //" http://mybatis.org/dtd/mybatis-3-config.dtd "> <configuration> <! -- After Spring is integrated with myBatis, this configuration file is basically optional --> <! -- Set the external configuration file --> <! -- Set category name --> <! -- Set the database connection environment --> <! -- Ing file --> </configuration>
VII. Test
@ RunWith (SpringJUnit4ClassRunner. class) // use the Springtest test framework @ ContextConfiguration ("/beans. xml ") // load the public class SMTest {@ Autowired // inject private UserMapper userMapper; @ Testpublic void save () {User user = new User (); user. setBirthday (new Date (); user. setName ("marry"); user. setSalary (300); userMapper. save (user); System. out. println (user. getId ();} @ Testpublic void update () {User user = userMapper. findById (2); user. setSalary (2000); userMapper. update (user) ;}@ Testpublic void delete () {userMapper. delete (3) ;}@ Testpublic void findById () {User user = userMapper. findById (1); System. out. println (user) ;}@ Testpublic void findAll () {List <User> users = userMapper. findAll (); System. out. println (users );}}