sql脚本
create database `lrx`; create table `user`{ `id` int primary key not null, `username` varchar(20), `password` varchar(20) }; insert into `user` (`id`,`username`,`password`) values(1,'lihua','123');
User.java
package com.pojo; public class User { private Integer id; private String username; private String password; public Integer getId() { return id; } public User(Integer id, String username, String password) { this.id = id; this.username = username; this.password = password; } public void setId(Integer id) { this.id = id; } 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; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + ''' + ", password='" + password + ''' + '}'; } }2、导入依赖
pom.xml
3、配置文件 mybatisConfig.xmljavaweb-maven lrx 1.0-SNAPSHOT 4.0.0 mybatis-01-buildEnvironmentmysql mysql-connector-java8.0.24 junit junit4.13.2 test org.mybatis mybatis3.5.7 src/main/java ***.xml true src/main/resources ***.xml true
4、新建UserMapper 接口并编写映射文件userMapper.xml
UserMapper.java
package com.mapper; import com.pojo.User; import java.util.List; public interface UserMapper { User select(int id); int addUser(User user); int update(User user); int delete(int id); }
userMapper.xml
5、新建 Mybatis单例类
新建 Mybatis.java 类文件,实例化它,使其持有一个 SqlSessionFactory 单例对象:
package com.utils; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; public class Mybatis { static SqlSessionFactory sqlSessionFactory = null; static { String resource = "mybatisConfig.xml"; InputStream inputStream = null; try { inputStream = Resources.getResourceAsStream(resource); } catch (IOException e) { e.printStackTrace(); } sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(); } }6、 UserService 类
UserService.java
package com.services; import com.pojo.User; import com.utils.Mybatis; import org.apache.ibatis.session.SqlSession; public class UserService { public User select(int id) { SqlSession sqlSession = Mybatis.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); System.out.println(mapper.select(id)); sqlSession.close(); } public int addUser(User user){ SqlSession sqlSession = Mybatis.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); int result=mapper.addUser(user); sqlSession.commit(); sqlSession.close(); return result } public int update(User user){ SqlSession sqlSession = Mybatis.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); int result=mapper.update(user); sqlSession.commit(); sqlSession.close(); return result; } public int delete(int id){ SqlSession sqlSession = Mybatis.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); int result=mapper.delete(id); sqlSession.commit(); sqlSession.close(); return result; } }7、新建一个 JUnit 测试类来测试 UserService
UserMapperTest.java
import java.util.*; import org.junit.*; import entity.Student; import services.StudentService; public class UserMapperTest { private static UserService userService; @BeforeClass public static void setup() { userService = new UserService(); } @AfterClass public static void teardown() { userService = null; } @Test public void testSelect() { User user = userService.select(1); System.out.println(user); } @Test public void testAddUser() { System.out.println(userService.addUser(new User(1,"小智","123"))); } @Test public void testUpdate() { System.out.println(userService.update(new User(1,"小红","456"))); } @Test public void testDelete() { System.out.println(userService.delete(1)); } }8、工作原理
首先,我们配置了 MyBatis 最主要的配置文件-mybatis-config.xml,里面包含了 JDBC 连接参数;配置了映射器Mapper XML 配置文件文件,里面包含了 SQL 语句的映射。
我们使用 mybatis-config.xml 内的信息创建了 SqlSessionFactory 对象。每个数据库环境应该就一个SqlSessionFactory 对象实例,所以我们使用了单例模式只创建一个 SqlSessionFactory 实例。
我们创建了一个映射器 Mapper 接口UserMapper,其定义的方法签名和在 UserMapper.xml 中定义的完全一样(即映射器 Mapper 接口中的方法名跟 UserMapper.xml 中的 id 的值相同)。注意 UserMapper.xml 中namespace 的值被设置成 com.mybatis3.mappers.UserMapper,是 UserMapper接口的完全限定名。这使我们可以使用接口来调用映射的 SQL 语句。
在 UserService.java 中,我们在每一个方法中创建了一个新的 SqlSession,并在方法功能完成后关闭SqlSession。每一个线程应该有它自己的 SqlSession 实例。SqlSession 对象实例不是线程安全的,并且不被共享。所以 SqlSession 的作用域最好就是其所在方法的作用域。从 Web 应用程序角度上看,SqlSession 应该存在于 request 级别作用域上。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)