使用idea构建maven
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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- DI注解 --> <context:annotation-config/> <!-- IoC注解 --> <context:component-scan base-package="com.demo.service" /> <context:property-placeholder location="classpath:db.properties" system-properties-mode="NEVER"/> <!--配置数据源--> <bean id = "myDataSource" class = "com.alibaba.druid.pool.DruidDataSource" init-method = "init" destroy-method = "close"> <property name = "DriverClassName" value = "${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!-- 配置sqlsessionfactory --> <bean id= "mySqlSessionFactory" class= "org.mybatis.spring.SqlSessionFactoryBean"> <property name= "dataSource" ref= "myDataSource" /> <property name= "configLocation" value= "classpath:Mybatis-config.xml" /> <property name= "typeAliasesPackage" value= "com.demo" /> <property name="mapperLocations" value="classpath:mappers/*Mapper.xml"/> </bean> <!-- 配置Mapper接口扫描器--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.demo.mapper"/> </bean> </beans>
Mybatis-config.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> <settings> <setting name="lazyLoadingEnabled" value="true"/> <setting name="aggressiveLazyLoading" value="false"/> <setting name="lazyLoadTriggerMethods" value="clone"/> </settings> </configuration>
db.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test jdbc.username=root jdbc.password=123
mvc-xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <import resource="classpath:applicationContext.xml"/> <!-- Ioc注解解析器 --> <context:component-scan base-package="com.demo.controller"></context:component-scan> <!-- 注解开发解析器 (自动注册上述配置)--> <mvc:annotation-driven> <!-- Json数据操作 --> <mvc:message-converters> <bean class="org.springframework.http.converter.json.GsonHttpMessageConverter"> <property name="supportedMediaTypes" value="application/json;charset=utf-8"></property> </bean> </mvc:message-converters> </mvc:annotation-driven> </beans>
log4j.properties(日志记录)
# priority :debug<info<warn<error #you cannot specify every priority with different file for log4j log4j.rootLogger=debug,stdout,info,debug,warn,error #console log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern= [%d{yyyy-MM-dd HH:mm:ss a}]:%p %l%m%n #info log log4j.logger.info=info log4j.appender.info=org.apache.log4j.DailyRollingFileAppender log4j.appender.info.DatePattern='_'yyyy-MM-dd'.log' log4j.appender.info.File=./src/com/hp/log/info.log log4j.appender.info.Append=true log4j.appender.info.Threshold=INFO log4j.appender.info.layout=org.apache.log4j.PatternLayout log4j.appender.info.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss a} [Thread: %t][ Class:%c >> Method: %l ]%n%p:%m%n #debug log log4j.logger.debug=debug log4j.appender.debug=org.apache.log4j.DailyRollingFileAppender log4j.appender.debug.DatePattern='_'yyyy-MM-dd'.log' log4j.appender.debug.File=./src/com/hp/log/debug.log log4j.appender.debug.Append=true log4j.appender.debug.Threshold=DEBUG log4j.appender.debug.layout=org.apache.log4j.PatternLayout log4j.appender.debug.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss a} [Thread: %t][ Class:%c >> Method: %l ]%n%p:%m%n #warn log log4j.logger.warn=warn log4j.appender.warn=org.apache.log4j.DailyRollingFileAppender log4j.appender.warn.DatePattern='_'yyyy-MM-dd'.log' log4j.appender.warn.File=./src/com/hp/log/warn.log log4j.appender.warn.Append=true log4j.appender.warn.Threshold=WARN log4j.appender.warn.layout=org.apache.log4j.PatternLayout log4j.appender.warn.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss a} [Thread: %t][ Class:%c >> Method: %l ]%n%p:%m%n #error log4j.logger.error=error log4j.appender.error = org.apache.log4j.DailyRollingFileAppender log4j.appender.error.DatePattern='_'yyyy-MM-dd'.log' log4j.appender.error.File = ./src/com/hp/log/error.log log4j.appender.error.Append = true log4j.appender.error.Threshold = ERROR log4j.appender.error.layout = org.apache.log4j.PatternLayout log4j.appender.error.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss a} [Thread: %t][ Class:%c >> Method: %l ]%n%p:%m%n
UserMapper.java
package com.demo.mapper; import com.demo.po.User; import java.util.List; public interface UserMapper { int insert (User u); int updateById(User u); int deleteById(int id); User selectById(int id); List<User> selectAll(); int login(User u); }
UserMapper.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.demo.mapper.UserMapper"> <resultMap id="BaseResultMap" type="User"> <id property="id" column="id" /> <result property="username" column="username" /> <result property="password" column="password" /> </resultMap> <select id="selectAll" resultMap="BaseResultMap"> SELECT id,username FROM user </select> <select id="selectById" resultMap="BaseResultMap"> SELECT id,username FROM user WHERE id = #{id} </select> <insert id="insert" useGeneratedKeys="true" keyProperty="id"> INSERT INTO user (username,password) VALUES (#{username},#{password}) </insert> <update id="updateById"> UPDATE user SET username = #{username} WHERE id= #{id} </update > <delete id= "deleteById"> DELETE FROM user WHERE id = #{id} </delete> <select id="login" resultType="Int"> SELECT count(*) FROM user WHERE username = #{username} and password = #{password} </select> </mapper>
IUserService.java
package com.demo.service; import com.demo.po.User; import java.util.List; public interface IUserService { int insert (User u); int updateById(User u); int deleteById(int id); User selectById(int id); List<User> selectAll(); int login(User u); }
UserServiceImpl.java
package com.demo.service; import com.demo.mapper.UserMapper; import com.demo.po.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; @Service @Transactional public class UserServiceImpl implements IUserService{ @Autowired private UserMapper userMapper; public int insert(User u) { return userMapper.insert(u); } public int updateById(User u) { return userMapper.updateById(u); } public int deleteById(int id) { return userMapper.deleteById(id); } public User selectById(int id) { return userMapper.selectById(id); } public List<User> selectAll() { return userMapper.selectAll(); } public int login(User u){ return userMapper.login(u); } }
UserController.java
package com.demo.controller; import com.demo.po.User; import com.demo.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController @CrossOrigin(origins ="*", maxAge =3600) @RequestMapping("/user") public class UserController { @Autowired private IUserService service; // 查询全部 @RequestMapping("/list") public List<User> list(Model model){ List<User> findAll = service.selectAll(); return findAll; } // 查询单个 @RequestMapping(value = "/get", method = RequestMethod.GET, produces = "application/json;charset=UTF-8") public Object get(@RequestParam(value = "id", required = false) int id){ User us=service.selectById(id); return us; } // 更新信息 @PostMapping("/update") public int update(@RequestBody User user){ System.out.println(user); return service.updateById(user); } // 插入信息 @PostMapping("/insert") public int inset(@RequestBody User user){ System.out.println(user); return service.insert(user); } // 删除信息 @GetMapping("/delete") public int inset(@RequestParam int id){ System.out.println(id); return service.deleteById(id); } // 登陆验证 @PostMapping("/login") public int login(@RequestBody User user){ return service.login(user); } }
User.java
public class User{ private int id; private String username; private String password; 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 getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + '}'; } }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>