Eclipse+Mybatis3.4+log4j1.2+mysql5.7
使用新手入门学习Mybatis时实例模板。
1.打开EClipse,File--new--java Project,名字叫做MybatisTemplate,点击Finish。
2.然后右击项目--Build Path--Config Build Path
3.选择Libraries下的Add External JARs
4.选择mybatis的jar包、log4j的jar包、连接mysql所需要的jar包
jar包下载:https://github.com/badaoliumang/jarPackageCollection
5.然后点击Apply--Apply and Close
6.新建包,目录如下
7.新建配置文件log4j.properties(log4j日志配置文件)、mybatis-config.xml(mybatis配置文件),目录如下
1.新建数据库ssmTemplate
2.新建表User,字段如下,注意id是自增主键。
1.pojo包下新建User.java
package com.badao.ssm.ssmTemplate.pojo;
public class User {
private int id;
private String userName;
private String remark;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
}
2.mapper包新建UserMapper.java接口
package com.badao.ssm.ssmTemplate.mapper;
import java.util.List;
import com.badao.ssm.ssmTemplate.pojo.User;
public interface UserMapper {
public int insertUser(User user);
public int deleteUser(int id);
public int updateUser(User user);
public User getUser(int id);
public List findUsers(String userName);
}
3.mapper包下新建UserMapper.xml映射文件
insert into User(userName, remark) values(#{userName}, #{remark})
delete from User where id= #{id}
update User set userName = #{userName}, remark = #{remark} where id= #{id}
其中namespace属性要与接口的全路径一样,即包名加接口名。
注意id为自增主键,所以针对于mybatis要配置:
seGeneratedKeys="true" keyProperty="id"
4.配置mybatis-config.xml
注:
别名配置是为了以后在别的地方用的时候不用写太长的别名。
映射文件要与上面的mapper文件所在的位置相对应。
5.配置log4j.properties
log4j.rootLogger=DEBUG , stdout
log4j.logger.org.mybatis=DEBUG
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d %C: %m%n
6.配置SqlSessionFactoryUtils类
package com.badao.ssm.ssmTemplate.utils;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class SqlSessionFactoryUtils {
private final static Class LOCK = SqlSessionFactoryUtils.class;
private static SqlSessionFactory sqlSessionFactory = null;
private SqlSessionFactoryUtils() {
}
public static SqlSessionFactory getSqlSessionFactory() {
synchronized (LOCK) {
if (sqlSessionFactory != null) {
return sqlSessionFactory;
}
String resource = "mybatis-config.xml";
InputStream inputStream;
try {
inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
return null;
}
return sqlSessionFactory;
}
}
public static SqlSession openSqlSession() {
if (sqlSessionFactory == null) {
getSqlSessionFactory();
}
return sqlSessionFactory.openSession(true);
}
}
1.新建MybatisMain.java 用于测试CRUD
package com.badao.ssm.ssmTemplate.main;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import com.badao.ssm.ssmTemplate.mapper.UserMapper;
import com.badao.ssm.ssmTemplate.pojo.User;
import com.badao.ssm.ssmTemplate.utils.SqlSessionFactoryUtils;
public class MybatisMain {
public static void main(String[] args) {
testUserMapper();
}
private static void testUserMapper() {
Logger log = Logger.getLogger(MybatisMain.class);
SqlSession sqlSession = null;
try {
sqlSession = SqlSessionFactoryUtils.openSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//测试查询
/*Role role = roleMapper.getRole(1l);
log.info(role.getRoleName());*/
//测试更新
/*Role role3 = new Role();
role3.setId(1l);
role3.setRoleName("YSH");
role3.setNote("测试更新");
int updateResult = roleMapper.updateRole(role3);*/
//测试插入
/*testInsert();
User user =new User();
user.setUserName("霸道");
user.setRemark("测试");
int insertResult = userMapper.insertUser(user);
if(insertResult == 0) {
System.out.println("插入失败");
log.info("插入失败");
}else {
System.out.println("插入成功");
log.info("插入成功");
}*/
//测试查询
/*User user2 = userMapper.getUser(1);
log.info("查询到的用户名为:"+user2.getUserName());*/
//测试更新
/*User user3 = new User();
user3.setId(1);
user3.setUserName("霸道------");
user3.setRemark("更新成功了");
int updateResult = userMapper.updateUser(user3);
if(updateResult == 0) {
System.out.println("更新失败");
log.info("更新失败");
}else {
System.out.println("更新成功");
log.info("更新成功");
}*/
//测试删除
/*int deleteResule = userMapper.deleteUser(1);
if(deleteResule == 0) {
System.out.println("删除失败");
log.info("删除失败");
}else {
System.out.println("删除成功");
log.info("删除成功");
}*/
//测试模糊查询
/*List userList = userMapper.findUsers("霸");
for (User user : userList) {
System.out.println(user.getUserName());
}*/
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}
private static void testInsert() {
// TODO Auto-generated method stub
}
}
github:
https://github.com/badaoliumang/SSMCodeCollection/tree/master/MybatisTemplate
项目总目录