之前写过的mybatis博客作为学习mybatis、spring还是不太合适。
现在找到一个不错的例子,首先将这个完整的mybatis增删查改例子在本地上实现出来,然后再进行学习。
项目结构与运行结果图:
pom文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.maven</groupId> <artifactId>mybatisCRUD</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>mybatisCRUD Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <!-- JUnit testing framework --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- Spring framework --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.2.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>3.2.6.RELEASE</version> </dependency> <!-- Spring AOP dependency --> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2</version> </dependency> <!-- MySQL database driver --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.0.5</version> </dependency> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>antlr</groupId> <artifactId>antlr</artifactId> <version>2.7.7</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.3</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.1</version> </dependency> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.7.4</version> </dependency> </dependencies> <build> <finalName>mybatisCRUD</finalName> </build> </project>
代码如下:
User.java
package com.mybatis; public class User { private int userId; private String userName; private String password; private String comment; public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; } }
UserDao.java
package com.mybatis; import java.util.List; public interface UserDao { public int insert(User user); public int update(User user); public int delete(String userName); public List<User> selectAll(); public int countAll(); public User findByUserName(String userName); }
MyBatis-Configuration.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <mappers> <mapper resource="com/mybatis/UserDaoMapper.xml"/> </mappers> </configuration>
UserDaoMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.mybatis.UserDao"> <select id="countAll" resultType="int"> select count(*) c from user; </select> <select id="selectAll" resultType="com.mybatis.User"> select * from user order by userName asc </select> <insert id="insert" parameterType="com.mybatis.User"> insert into user(userName,password,comment) values(#{userName},#{password},#{comment}) </insert> <update id="update" parameterType="com.mybatis.User"> update user set userName=#{userName},password=#{password},comment=#{comment} where userName=#{userName} </update> <delete id="delete" parameterType="int"> delete from user where userName=#{userName} </delete> <select id="findByUserName" parameterType="String" resultType="com.mybatis.User"> select * from user where userName=#{userName} </select> </mapper>
applicationContext.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/hlp?useUnicode=true&characterEncoding=UTF-8"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> <property name="maxActive" value="100"></property> <property name="maxIdle" value="30"></property> <property name="maxWait" value="500"></property> <property name="defaultAutoCommit" value="true"></property> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="com/mybatis/MyBatis-Configuration.xml"></property> <property name="dataSource" ref="dataSource" /> </bean> <bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.mybatis.UserDao"></property> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> </beans>
Test.java
package com.mybatis; import org.springframework.context.ApplicationContext; import com.mybatis.User; import java.io.Reader; import java.util.Iterator; import java.util.List; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserDao userDao=(UserDao)context.getBean("userDao"); System.out.println(userDao.countAll()); User user=new User(); user.setUserName("虚"); user.setPassword("1256"); user.setComment("注"); userDao.insert(user); List<User> users = userDao.selectAll(); //mybatis java orm Iterator<User> iter = users.iterator(); while(iter.hasNext()){ User u = iter.next(); System.out.println("用户名:"+u.getUserName()+"密码:"+u.getPassword()); } user.setComment("comment"); userDao.update(user); userDao.delete("虚"); System.out.println("记录条数:"+userDao.countAll()); List<User> users2 = userDao.selectAll(); Iterator<User> iter2 = users2.iterator(); while(iter2.hasNext()){ User u2 = iter2.next(); System.out.println("用户名:"+u2.getUserName()+"密码:"+u2.getPassword()); } System.out.println("hello,this is my first mybatis program"); } }
上图和下图是该程序所使用的数据库图