基于maven采用mybatis实现简单的增删查改

​ Mybatis 01

一、在pom.xml中配置mybatis:

 <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatisartifactId>
      <version>3.4.6version>
    dependency>

二、在pom.xml中配置mysql驱动:

<dependency>
      <groupId>mysqlgroupId>
      <artifactId>mysql-connector-javaartifactId>
      <version>5.1.6version>  
    dependency>

三、在java下面的pojo包里面写User.java,并生成getter、setter方法:

public class User {
    private int Uid;
    private String Uname;
    private String Upassword;
    private String Uphone;

    @Override
    public String toString() {
        return "User{" +
                "Uid=" + Uid +
                ", Uname='" + Uname + '\'' +
                ", Upassword='" + Upassword + '\'' +
                ", Uphone='" + Uphone + '\'' +
                '}';
    }

    public int getUid() {
        return Uid;
    }

    public void setUid(int uid) {
        Uid = uid;
    }

    public String getUname() {
        return Uname;
    }

    public void setUname(String uname) {
        Uname = uname;
    }

    public String getUpassword() {
        return Upassword;
    }

    public void setUpassword(String upassword) {
        Upassword = upassword;
    }

    public String getUphone() {
        return Uphone;
    }

    public void setUphone(String uphone) {
        Uphone = uphone;
    }
}

四、在resources下面写sqlMapConfig.xml:



<configuration>
    
    <properties resource="properties/db.properties">properties>

    <settings>
        
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    settings>

    
    <typeAliases>
        
        <typeAlias type="java.util.List" alias="list"/>
        
        <package name="pojo"/>
    typeAliases>

    
    


    
    <environments default="mysql">
        <environment id="mysql">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            dataSource>
        environment>
    environments>

    
    
    <mappers>
        <mapper resource="mapper/UserMapper.xml"/>
    mappers>
configuration> 

五、在resources建立一个properties文件夹,写db.properties:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test2?useSSL=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
jdbc.username=root
jdbc.password=123456

六、在resources建立一个mapper文件夹,写映射文件UserMapper.xml:




<mapper namespace="mapper.IUserDao">
    
     
    <select id="selectUsers" resultType="user">
        select * from user_info
    select>
	
    <insert id="addUser" parameterType="user">
        insert into user_info VALUES (NULL ,#{Uname},#{Upassword},#{Uphone})
    insert>
	
    <delete id="deleteUser" parameterType="int">  
        DELETE from user_info where Uid=#{id}
    delete>
    <delete id="deleteUser1" parameterType="user">   
        DELETE from user_info where Uname=#{Uname}
    delete>
    
    <update id="updateUer" parameterType="user">
        UPDATE user_info SET Uname=#{Uname} where Uid=#{Uid}
    update>
mapper>

七、在java包下建立一个mapper包,里面写接口IUserDao.java:

public interface IUserDao {
    //查
    public List<User> selectUsers();
	//增
    public int addUser(User user);
	//以id为条件
    public int deleteUser(int id);
	//以User类的Uname属性为条件
    public int deleteUser1(User user);
	//改
    public int updateUer(User user);
}

八、新建一个文件夹test和java、resources同级,并设置为Test Source Root,里面写测试类,可直接用Junit里面的@Test、@After、@Before来写测试类:

public class Test1 {
    SqlSession sqlSession;
    @Before
    public void init(){
        try {
            //读取核心配置文件sqlMapConfig.xml
            InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
            //找一个盖工厂的建筑师
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            //构建工厂
            SqlSessionFactory build = builder.build(inputStream);
            //工厂生产sqlSession对象
            sqlSession = build.openSession();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    //查
    public void select(){
        //sqlSession对象代理增删查改
        List<User> users = sqlSession.getMapper(IUserDao.class).selectUsers();
        System.out.println(users);
    }
    //增
	@Test
    public void add(){
    User user = new User();
    user.setUname("天天");
    user.setUpassword("123456");
    user.setUphone("1047896512");
        //sqlSession对象代理增删查改
    int i = sqlSession.getMapper(IUserDao.class).addUser(user);
    System.out.println(i);
	}
    //改
	@Test
	public void update(){
    User user = new User();
    user.setUname("图图");
    user.setUid(2);
        //sqlSession对象代理增删查改
    int i = sqlSession.getMapper(IUserDao.class).updateUer(user);
	}
	//删
	@Test
	public void delete(){
      //以id为条件
    int i = sqlSession.getMapper(IUserDao.class).deleteUser(8); //sqlSession对象代理增删查改
      //以User类的Uname属性为条件
    System.out.println(i);
    User user = new User();
    user.setUname("maomao");
        //sqlSession对象代理增删查改
    int i1 = sqlSession.getMapper(IUserDao.class).deleteUser1(user); 
    System.out.println(i1);
	}
	@After
    public void destroy(){
        try {
            sqlSession.commit();
        }catch (Exception e){
            sqlSession.rollback();
        }finally {
            sqlSession.close();
        }
    }
}

你可能感兴趣的:(SSM框架)