java_Web 实战06
java_Web 实战06
在实现功能的同时要做前端的jsp和后端的servlet方面的处理,同时这里使用scv三层架构,所以要书写secvice层和dao层数据库的mapper的代码,在书写mapper时要配置xml文件
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///db3?useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/home/mapper/UserMapper.xml"></mapper>
<mapper resource="com/home/mapper/ShopperMapper.xml"></mapper>
<mapper resource="com/home/mapper/HouseMapper.xml"></mapper>
<mapper resource="com/home/mapper/AgentMapper.xml"></mapper>
</mappers>
</configuration>
package com.home.mapper;
import com.home.pojo.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
public interface UserMapper {
@Select("select * from tb_user where username=#{username} and password=#{password} ")
User Login(User user);
@Insert("insert into tb_user values (#{username},#{password},#{type})")
void insertUser(User user);
@Select("select * from tb_user")
List<User> selectUser();
@Update("UPDATE tb_user set type='0' where username=#{username}")
void approval(String username);
@Update("update tb_user set password=#{password} where username=#{username}")
void updatePassword(User user);
}
这里使用注解开发,大大提高了代码的书写效率
package com.home.service;
import com.home.mapper.UserMapper;
import com.home.pojo.User;
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;
import java.util.List;
public class UserService {
public User login(User user) throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(com.home.mapper.UserMapper.class);
User user1=userMapper.Login(user);
sqlSession.close();
// System.out.println(user1);
return user1;
}
public void insertUser(User user) throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(com.home.mapper.UserMapper.class);
userMapper.insertUser(user);
sqlSession.commit();
sqlSession.close();
}
public List<User> selectUser() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(com.home.mapper.UserMapper.class);
List<User>users=userMapper.selectUser();
sqlSession.close();
return users;
}
public void approval(String userId) throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(com.home.mapper.UserMapper.class);
userMapper.approval(userId);
sqlSession.commit();
sqlSession.close();
}
public void updatePassword(User user) throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(com.home.mapper.UserMapper.class);
userMapper.updatePassword(user);
sqlSession.commit();
sqlSession.close();
}
}
同时用service层来封装对于数据库的操作,使得代码的可维护性更加强大